简体   繁体   English

将roboelectric与android studio与gradle集成总是给出包org.junit不存在错误

[英]Integrating roboelectric with android studio with gradle always gives package org.junit does not exist error

I am trying to integrate roboelectric in my new project. 我正在尝试将roboelectric整合到我的新项目中。 I used following links to understand and impliment roboelectric 我使用以下链接来理解和实施roboelectric

http://pivotallabs.com/setting-up-robolectric-in-android-studio-1-1-on-os-x/ http://pivotallabs.com/setting-up-robolectric-in-android-studio-1-1-on-os-x/

http://nenick-android.blogspot.in/2015/02/android-studio-110-beta-4-and.html http://nenick-android.blogspot.in/2015/02/android-studio-110-beta-4-and.html

http://raptordigital.blogspot.in/2014/02/test-driven-development-with.html http://raptordigital.blogspot.in/2014/02/test-driven-development-with.html

But I am gettin following error on running my test class 但是我在运行我的测试类时遇到错误

Error:Gradle: Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.
E:\junitTest\android-studio-robolectric-example-master\JunitTestApp\app\src\test\java\com\inapp\junittestapp\SampleTest.java
Error:(7, 17) Gradle: error: package org.junit does not exist
 Error:(8, 24) Gradle: error: package org.junit.runner does not exist
Error:(9, 23) Gradle: error: package org.robolectric does not exist
 Error:(10, 34) Gradle: error: package org.robolectric.annotation does not exist
Error:(11, 31) Gradle: error: package org.robolectric.shadows does not exist
Error:(13, 27) Gradle: error: package org.hamcrest does not exist
Error:(13, 1) Gradle: error: static import only from classes and interfaces
Error:(14, 27) Gradle: error: package org.hamcrest does not exist
Error:(14, 1) Gradle: error: static import only from classes and interfaces
Error:(15, 24) Gradle: error: package org.junit does not exist
Error:(15, 1) Gradle: error: static import only from classes and interfaces
Error:(16, 30) Gradle: error: package org.robolectric does not exist
Error:(16, 1) Gradle: error: static import only from classes and interfaces
Error:(17, 30) Gradle: error: package org.robolectric does not exist
Error:(17, 1) Gradle: error: static import only from classes and interfaces
Error:(18, 30) Gradle: error: package org.robolectric does not exist
Error:(18, 1) Gradle: error: static import only from classes and interfaces
Error:(25, 2) Gradle: error: cannot find symbol class RunWith
Error:(26, 2) Gradle: error: cannot find symbol class Config
Error:(30, 6) Gradle: error: cannot find symbol class Test
Error:(32, 38) Gradle: error: cannot find symbol method      setupActivity(Class<MainActivity>)
Error:(36, 9) Gradle: error: cannot find symbol method clickOn(Button)
Error:(38, 41) Gradle: error: cannot find symbol variable ShadowAlertDialog
Error:(39, 42) Gradle: error: cannot find symbol method notNullValue()
Error:(41, 9) Gradle: error: cannot find symbol class ShadowAlertDialog
Error:(41, 47) Gradle: error: cannot find symbol method shadowOf(AlertDialog)
Error:(42, 61) Gradle: error: cannot find symbol method is(String)

Added below my test class code 在我的测试类代码下面添加了

import android.app.AlertDialog;
import android.widget.Button;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.annotation.Config;
import org.robolectric.shadows.ShadowAlertDialog;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;
import static org.robolectric.Robolectric.clickOn;
import static org.robolectric.Robolectric.setupActivity;
import static org.robolectric.RobolectricBase.shadowOf;


@RunWith(RobolectricTestRunner.class)
@Config(manifest = "app/src/main/AndroidManifest.xml", emulateSdk = 18)
public class SampleTest {

    @Test
    public void buttonTapDisplaysAnAlertDialog() {
        MainActivity helloActivity = setupActivity(MainActivity.class);

        Button button = (Button) helloActivity.findViewById(R.id.button);

        clickOn(button);

        AlertDialog latestAlertDialog = ShadowAlertDialog.getLatestAlertDialog();
        assertThat(latestAlertDialog, is(notNullValue()));

        ShadowAlertDialog shadowAlertDialog = shadowOf(latestAlertDialog);
        assertThat(shadowAlertDialog.getTitle().toString(), is("Hi"));
    }

}

and my app gradle file is 和我的应用程序gradle文件是

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "21.1.2"

    defaultConfig {
        applicationId "com.example.junittestapp"
        minSdkVersion 15
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
    sourceSets { main { java.srcDirs = ['src/main/java', 'src/test/java/'] } }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    compile 'com.android.support:support-v4:22.1.1'
    testCompile 'junit:junit:4.12'
    testCompile 'org.robolectric:robolectric:2.4'
}

I found same error in this blog too 我在这个博客中也发现了同样的错误

https://discuss.gradle.org/t/why-cant-my-simplest-one-line-build-gradle-compile-tests-with-junit-jar/1868 https://discuss.gradle.org/t/why-cant-my-simplest-one-line-build-gradle-compile-tests-with-junit-jar/1868

I think that I miss something. 我想我错过了什么。 Any help will be appreciated. 任何帮助将不胜感激。

Your problem is that you have one sourceSet declared as main that contains code for application and for test. 您的问题是您有一个声明为main的sourceSet ,其中包含应用程序和测试的代码。 That's why Gradle task ':app:compileDebugJava' fails because it doesn't have junit.jar at that moment (it's injected at testCompile - time). 这就是Gradle task':app:compileDebugJava'失败的原因,因为它当时没有junit.jar (它是在testCompile - 时注入的)。 Declare two different sourceSet's and everything should work. 声明两个不同的sourceSet,一切都应该有效。

sourceSets {
    main {
        java.srcDirs = ['src/main/java']
    }
    robolectric {
        java.srcDir file('src/test/java/')
        resources.srcDir file('src/test/resources')
    }
}

Same error here. 这里也是错误的。 The solution to my case is: remove 'src/test/java' from main.java.srcDirs . 我的解决方案是:从main.java.srcDirs删除'src/test/java'

Before : 之前

main.java.srcDirs = ['src/main/java', 'src/test/java']

After : 之后

main.java.srcDirs = ['src/main/java']

Tested OK with Android Studio 2.0 Preview 4. And actually with the latest Android Studio, it's not really necessary to set robolectric property inside sourceSets (as @falvick mentioned). 使用Android Studio 2.0 Preview 4进行测试。实际上,使用最新的Android Studio,并不一定需要在sourceSets中设置robolectric属性(如@falvick所述)。

If you got here by trying to run your unit tests in Android you can fix this issue by separately specifying the location of your test files in the sourceSets like so: 如果您通过尝试在Android中运行单元测试来到这里,您可以通过在sourceSets中单独指定测试文件的位置来解决此问题,如下所示:

sourceSets {
    main { java.srcDirs = ['src/main/java'] }
    test { java.srcDirs = ['src/test/java'] }
}

You can modify to use wherever your source directory is at. 您可以修改以在源目录所在的位置使用。 Note this goes in the build.gradle (app level) 请注意,这是在build.gradle(应用程序级别)

Had the same problem, in my case I placed the test class in the wrong directory. 有同样的问题,在我的情况下,我把测试类放在错误的目录中。

src\\androidTest\\java[package]\\ SRC \\ androidTest \\的java [包] \\

rather than 而不是

src\\test\\java[package]\\ SRC \\测试\\的java [包] \\

I guess you can configure it to work with other directories, but putting unit tests in test and UI/system tests in androidTest looks ok 我猜你可以将它配置为与其他目录一起使用,但是将单元测试放在androidTest中的测试和UI /系统测试中看起来没问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM