安卓@RunWith(AndroidJUnit4.class)-无法在包“androidTest”中解析

安卓@RunWith(AndroidJUnit4.class)-无法在包“androidTest”中解析,android,unit-testing,Android,Unit Testing,Android研究2.2.3 安装Android支持存储库-ver。44.0.0 我在官方网站上设置了所有浓缩咖啡: 我尝试在androidTest包中编写仪器测试浓缩咖啡。因此,我在src/androidTest/java/com/mycompany文件夹中创建了StringUtilAndroidTest/ 我的StringUtilAndroidTest代码: 我的依赖项: testCompile 'junit:junit:4.12' testCompile 'org.hamcrest:

Android研究2.2.3

安装Android支持存储库-ver。44.0.0

我在官方网站上设置了所有浓缩咖啡:

我尝试在androidTest包中编写仪器测试浓缩咖啡。因此,我在src/androidTest/java/com/mycompany文件夹中创建了StringUtilAndroidTest/

我的StringUtilAndroidTest代码:

我的依赖项:

   testCompile 'junit:junit:4.12'
testCompile 'org.hamcrest:hamcrest-library:1.3'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'com.android.support.test:testing-support-lib:0.1
但在StringUtilAndroidTest中,我得到编译错误:

@RunWith(AndroidJUnit4.class)
无法解析符号RunWith


为什么?

简短的回答:将此添加到您的依赖项中,您就是黄金

androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
长答覆:

在默认配置中,androidstudio项目有两个不同的测试变体:test和androidTest。前者使用“src/test/java”,后者使用“src/androidTest/java”,这是您的场景

两者之间有很大的区别:androidTest需要一个模拟器或设备来运行,而test则不需要。这意味着测试通常在IDE上运行几秒钟要快得多,但它无法访问Android框架,如活动、上下文等。另一方面,androidTest运行时间要长得多,更不用说模拟器本身的等待时间了,但它确实有Android框架,因为它在一个平台上运行

因为它们是两个独立的变体,所以您也需要分别声明它们的依赖关系。testCompile和androidTestCompile各自只将依赖项添加到自己的变体中。要在两者上都使用JUnit,您必须声明对这两者的依赖关系—实际上是重复这一行


注意:当您使用compile时,它会将其添加到所有变体中,因此您不必重复非测试依赖项。

简短回答:将此添加到依赖项中,您就成功了

androidTestCompile 'junit:junit:4.12'
androidTestCompile 'org.hamcrest:hamcrest-library:1.3'
长答覆:

在默认配置中,androidstudio项目有两个不同的测试变体:test和androidTest。前者使用“src/test/java”,后者使用“src/androidTest/java”,这是您的场景

两者之间有很大的区别:androidTest需要一个模拟器或设备来运行,而test则不需要。这意味着测试通常在IDE上运行几秒钟要快得多,但它无法访问Android框架,如活动、上下文等。另一方面,androidTest运行时间要长得多,更不用说模拟器本身的等待时间了,但它确实有Android框架,因为它在一个平台上运行

因为它们是两个独立的变体,所以您也需要分别声明它们的依赖关系。testCompile和androidTestCompile各自只将依赖项添加到自己的变体中。要在两者上都使用JUnit,您必须声明对这两者的依赖关系—实际上是重复这一行


注意:当您使用compile时,它会将其添加到所有变体中,因此您不必重复非测试依赖项。

您可能遗漏了一些依赖项

//App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'

// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'junit:junit:4.12'

testCompile 'junit:junit:4.12'

希望这能修复您的代码。

您可能缺少一些依赖项

//App's dependencies, including test
compile 'com.android.support:support-annotations:22.2.0'

// Testing-only dependencies
androidTestCompile 'com.android.support.test:runner:0.5'
androidTestCompile 'com.android.support.test.espresso:espresso-core:2.2.2'
androidTestCompile 'junit:junit:4.12'

testCompile 'junit:junit:4.12'

希望这能修复您的代码。

我刚刚测试过。在我的案例中,他们确实起了作用。我刚刚测试过。在我的例子中,它们确实起了作用。除了将androidTestCompile行添加到build.gradle之外,您还记得在Java文件的顶部导入该类吗?i、 e.导入org.junit.runner.RunWith;导入org.junit.runner.RunWith;-无法解析符号“RunWith”,除了将androidTestCompile行添加到build.gradle之外,您还记得在Java文件顶部导入该类吗?i、 e.导入org.junit.runner.RunWith;导入org.junit.runner.RunWith;-无法解析符号“RunWith”