在ExampleInstrumentedTest.java文件中获取错误

时间:2016-10-14 12:30:42

标签: java android eclipse android-studio junit

我的项目成功运行期望我清理并重建项目时 收到此错误

Information:Gradle tasks [clean, :app:generateDebugSources, :app:generateDebugAndroidTestSources, :app:mockableAndroidJar, :app:prepareDebugUnitTestDependencies, :app:compileDebugSources, :app:compileDebugAndroidTestSources, :app:compileDebugUnitTestSources]
D:\Gonna Projects\DoctorApps\app\src\androidTest\java\in\doctormobileapps\doctorapps\ExampleInstrumentedTest.java
Error:(4, 28) error: package android.support.test does not exist
Error:(5, 35) error: package android.support.test.runner does not exist
Error:(7, 17) error: package org.junit does not exist
Error:(8, 24) error: package org.junit.runner does not exist
Error:(11, 24) error: package org.junit does not exist
Error:(18, 2) error: cannot find symbol class RunWith
Error:(20, 6) error: incompatible types: Test cannot be converted to Annotation
Error:(23, 30) error: cannot find symbol variable InstrumentationRegistry
Error:Execution failed for task ':app:compileDebugAndroidTestJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
Information:BUILD FAILED
Information:Total time: 4.834 secs
Information:9 errors
Information:0 warnings
Information:See complete output in console

是否有人可以告诉我我做错了什么?

4 个答案:

答案 0 :(得分:5)

您似乎没有将Junit-Package添加到模块的 build.gradle 文件中。只需在依赖项块中添加它,它应该可以工作:

androidTestCompile 'junit:junit:4.12'
testCompile 'junit:junit:4.12'

答案 1 :(得分:4)

当我从头开始创建一个新模块时,Android studio将生成一个默认的gradle文件,其中包含以下与测试相关的依赖项:

androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', {
    exclude group: 'com.android.support', module: 'support-annotations'
})

这似乎可以解决构建日志中的至少一些依赖性问题。由于缺少org.junit引用,可能还需要将junit添加为androidTestCompile依赖项,因为@Chris已经回答了;或者它可能含在espresso中。

答案 2 :(得分:1)

您的gradle构建中可能缺少依赖项,将这些依赖项添加到行:

testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:0.5'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:2.2.2'

答案 3 :(得分:0)

我迁移到android x,并在ExampleInstrumentedTest.java类中发现了以下错误。

Error in ExampleInstrumentedTest.java

我将代码更改为

    Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();

解决了我的错误问题。