Search code examples
androidandroid-testing

Cast Exception - AndroidJUnitRunner cannot be cast to RoboMonitoringInstrumentation


Working on Android Instrumented Test to launch and test this fragment

enter image description here

But I am running to this nasty error.

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:153)
at org.robolectric.android.internal.LocalActivityInvoker.startActivity(LocalActivityInvoker.java:40)
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$Companion.internalLaunch$fragment_testing_release(FragmentScenario.kt:588)
at androidx.fragment.app.testing.FragmentScenario$Companion.launchInContainer(FragmentScenario.kt:559)
at com.example.android.architecture.blueprints.todoapp.taskdetail.TaskDetailFragmentTest.activeTasks_displayedInUi(TaskDetailFragmentTest.kt:35)Tests ran to completion.

These are the dependencies that I'm using

enter image description here

Have been looking for an answer online but I have not come to any useful finding.

What could I be missing?


Solution

  • You should check the Roboelectric Dependency and ensure it is testImplementation

    In my case I was using this version

    implementation "org.robolectric:robolectric:4.5.1
    

    The error was gone when I switched to test implementation.

    testImplementation "org.robolectric:robolectric:4.5.1"