【问题标题】:Getting error in ExampleInstrumentedTest.java file在 ExampleInstrumentedTest.java 文件中出现错误
【发布时间】:2017-02-23 21:20:09
【问题描述】:

我的项目成功运行,预计当我清理并重建项目时,我 收到此错误

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

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

【问题讨论】:

    标签: java android eclipse android-studio junit


    【解决方案1】:

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

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

    【讨论】:

    • 那么请提供您用于测试类的代码和您的 build.gradle 文件。
    • 解决了我的问题!由于某种原因,Android Studio 在生成 build.gradle 文件时没有包含 androidTestCompile 行...
    • Configuration 'androidTestCompile' is obsolete and has been replaced with 'androidTestImplementation' and 'androidTestApi'. It will be removed at the end of 2018. For more information see: http://d.android.com/r/tools/update-dependency-configurations.html
    【解决方案2】:

    当我从头开始创建新模块时,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 中。

    【讨论】:

      【解决方案3】:

      您的 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'
      

      【讨论】:

        【解决方案4】:

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

        我把代码改成了

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

        这解决了我的错误问题。

        【讨论】:

        • 这是我迁移到 android x 后的解决方案
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-04-15
        • 1970-01-01
        • 1970-01-01
        • 2016-09-01
        • 1970-01-01
        相关资源
        最近更新 更多