Android Espresso ActivityTest给出了“程序包android.support.test.rule不存在”

时间:2019-06-10 18:58:56

标签: android android-espresso android-testing

我正在使用此演示学习Android的TDD:

https://github.com/NileshJarad/TDD_Demo

我可以运行所有单元测试和Mockito测试,但是“ activityTests”由于以下错误而失败:

error: package android.support.test.rule does not exist 
error: package android.support.test.espresso does not exist 
error: package android.support.test.espresso.action does not exist  
error: package android.support.test.espresso.action does not exist  
error: package android.support.test.espresso.action does not exist  
error: package android.support.test.espresso.assertion does not exist   
error: package android.support.test.espresso.matcher does not exist 
error: package android.support.test.espresso.matcher does not exist 
error: package android.support.test.espresso.matcher does not exist 
error: cannot find symbol class ActivityTestRule    
error: cannot find symbol method isDisplayed()  
...

版本为Android 9.0 API 28。

我在https://www.google.com/search?q=Android+ActivityTest+gives+%27package+android.support.test.rule+does+not+exist%27尝试了建议,但它们都引用了我们已经拥有的app/build.gradle行:

...
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
...
dependencies {
...    
    testImplementation 'junit:junit:4.12'
    testImplementation 'org.mockito:mockito-core:1.10.19'
    androidTestImplementation 'com.android.support.test:rules:1.0.2'
    androidTestImplementation('androidx.test.espresso:espresso-core:3.1.0', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    androidTestImplementation 'com.android.support.test:testing-support-lib:0.1'
}

1 个答案:

答案 0 :(得分:2)

我认为您只需要将“规则”依赖项更改为AndroidX版本:

androidTestImplementation 'androidx.test:rules:1.2.0'

同步项目后,您需要将测试更改为使用androidx.test.*而不是android.support.test.*(可以通过删除灰色的import语句然后接受Android Studio建议的所有新导入)。

此时,您的测试应该运行了!

另一件事:现在,您可以删除测试类顶部的注释,其中“ AndroidJUnit4”被标记为已弃用(即划掉):

@RunWith(AndroidJUnit4.class)

According to the documentation@RunWith现在仅在同时使用JUnit3JUnit4(您的测试不需要)的情况下才需要。