android - java.lang.ClassCastException : androidx. test.runner.AndroidJUnitRunner 无法转换为 org.robolectric.android.fakes.RoboMonitoringInstrumentation

标签 android android-espresso robolectric

我正在测试 android UI 组件,并且我已经导入了以下依赖项。我使用 espresso 和其他库进行 UI 组件测试。尝试了多种方法,但无法进一步进行。请找到下面提到的所需 fragment 。

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)

最佳答案

有类似的问题,我必须删除 robolectric来自 androidTestImplementation所以测试成功运行。在您的情况下,请尝试删除此导入:

androidTestImplementation("org.robolectric:robolectric:4.3.1") {
    exclude group: "org.apache.maven.wagon"
    exclude group: "org.apache.maven"
}
https://stackoverflow.com/a/61838248/2173733作为引用:)
*编辑:还要确保您的测试代码在/androidTest 下

关于android - java.lang.ClassCastException : androidx. test.runner.AndroidJUnitRunner 无法转换为 org.robolectric.android.fakes.RoboMonitoringInstrumentation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61745532/

相关文章:

robolectric - Dagger2在Test中不生成组件

java - Espresso Android 匹配父级中的 TextView

android - robolectric 2 - 创建有 Intent 的测试 Activity

android - Android 上持续的蓝牙 LE 信号强度

android - 在我的应用程序中控制默认自动旋转屏幕

java - Espresso : returning to application

android - 注入(inject)模拟对象时测试仪器进程崩溃

android - 在使用 Robolectric 测试 Android 应用程序时,如何创建影子类来解决与自定义属性相关的崩溃问题?

android - 如何在 Google map 上放置图钉以及如何在 Google map 上的对话框中获取当前地址?

Android facebook sdk 4.0 ProfileTracker onCurrentProfileChanged 永远不会被调用