java.lang.ClassCastException: androidx.test.runner.AndroidJUnitRunner不能被投到org.robolectric.android.fakes.RoboMonitoringInstrumentation。

6 人关注

我正在测试android UI组件,我已经导入了以下依赖。我已经使用espresso和其他库来测试UI组件。我已经尝试了多种方法,但无法继续下去。请找到下面提到的所需的片段。

implementation "androidx.annotation:annotation:1.1.0"
implementation "com.google.guava:guava:29.0-android"
androidTestImplementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation "androidx.navigation:navigation-testing:2.3.0-alpha06"
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
androidTestImplementation "androidx.test:core:1.3.0-beta01"
androidTestImplementation "androidx.test:core-ktx:1.3.0-beta01"
androidTestImplementation "androidx.test.ext:junit-ktx:1.1.2-beta01"
androidTestImplementation "androidx.test:runner:1.3.0-beta01"
androidTestImplementation ("org.robolectric:robolectric:4.3.1") {
    exclude group: "org.apache.maven.wagon"
    exclude group: "org.apache.maven"
androidTestImplementation "androidx.test.ext:truth:1.3.0-beta01"
testImplementation "androidx.test:core:1.3.0-beta01"
testImplementation "androidx.test.ext:junit:1.1.2-beta01"
testImplementation ("org.robolectric:robolectric:4.3.1"){
    exclude group: "org.apache.maven.wagon"
    exclude group: "org.apache.maven"
testImplementation "androidx.test.espresso:espresso-core:3.3.0-beta01"
testImplementation "androidx.test.espresso:espresso-intents:3.3.0-beta01"
testImplementation "androidx.test.ext:truth:1.3.0-beta01"
// Test libraries
testImplementation 'junit:junit:4.13'
testImplementation 'org.mockito:mockito-core:2.22.0'
// alternatively - without Android dependencies for testing
testImplementation "androidx.paging:paging-common:$paging_version"
// Test helpers
testImplementation "androidx.room:room-testing:$room_version"
testImplementation "com.squareup.okhttp3:mockwebserver:4.6.0"
def fragment_version = "1.2.4"
debugImplementation "androidx.fragment:fragment-testing:$fragment_version"
debugImplementation "androidx.test:core:1.3.0-beta01"
debugImplementation "androidx.test:monitor:1.3.0-beta01"

我写了以下测试类来测试UI组件

@RunWith(AndroidJUnit4::class)
class FragmentNavigationTest {
@Test
fun testNavigationToHomeScreen() {
    // Set navigation graph
    val navController = TestNavHostController(ApplicationProvider.getApplicationContext())
    navController.setGraph(R.navigation.app_nav)
    val splashFragment = launchFragmentInContainer<SplashFragment>()
    splashFragment.onFragment { fragment ->
        Navigation.setViewNavController(fragment.requireView(), navController)
    ShadowLooper.runUiThreadTasksIncludingDelayedTasks()
    assertThat(navController.currentDestination?.id).isEqualTo(R.id.moviesHomeFragment)

我得到了以下错误

java.lang.ClassCastException: androidx.test.runner.AndroidJUnitRunner cannot be cast to org.robolectric.android.fakes.RoboMonitoringInstrumentation
at org.robolectric.android.internal.LocalActivityInvoker.getInstrumentation(LocalActivityInvoker.java:147)
at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:34)
at androidx.test.core.app.ActivityScenario.launchInternal(ActivityScenario.java:265)
at androidx.test.core.app.ActivityScenario.launch(ActivityScenario.java:226)
at androidx.fragment.app.testing.FragmentScenario.internalLaunch(FragmentScenario.java:299)
at androidx.fragment.app.testing.FragmentScenario.launchInContainer(FragmentScenario.java:282)
at com.myomdbapplication.FragmentNavigationTest.testNavigationToHomeScreen(FragmentNavigationTest.kt:36)
at java.lang.reflect.Method.invoke(Native Method)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at androidx.test.ext.junit.runners.AndroidJUnit4.run(AndroidJUnit4.java:154)
at org.junit.runners.Suite.runChild(Suite.java:128)
at org.junit.runners.Suite.runChild(Suite.java:27)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
at org.junit.runner.JUnitCore.run(JUnitCore.java:115)
at androidx.test.internal.runner.TestExecutor.execute(TestExecutor.java:56)
at androidx.test.runner.AndroidJUnitRunner.onStart(AndroidJUnitRunner.java:395)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2189)
    
3 个评论
gosr
你在gradle文件中定义了一个 testInstrumentationRunner 吗?
对此有什么解决办法吗?
android
android-espresso
robolectric
Prabhudas kuppala
Prabhudas kuppala
发布于 2020-05-12
1 个回答
gedeh
gedeh
发布于 2020-09-07
已采纳
0 人赞同

有类似的问题,我必须从 androidTestImplementation 中删除 robolectric ,这样测试才能成功运行。在你的情况下,尝试删除这个导入。

androidTestImplementation("org.robolectric:robolectric:4.3.1") {
    exclude group: "org.apache.maven.wagon"