I am testing android UI components and I have imported the following dependencies. I have used espresso and other libraries for UI components testing. Have tried multiple ways but unable to proceed further. Please find the required snippets mentioned bellow.
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"
I have written the below test class to test the UI components
@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)
}
}
I am getting the below 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: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)