android / codelab-android-navigation

Apache License 2.0
634 stars 275 forks source link

Facing Exception UI Testing || Android Navigation Testing #107

Open QumberAbbas12 opened 3 years ago

QumberAbbas12 commented 3 years ago

java.lang.IllegalStateException: Method addObserver must be called on the main thread. Line navController.setGraph(R.navigation.navigation_onboarding)

`@RunWith(AndroidJUnit4::class) class UserEmailFragmentTest {

@get:Rule
val activityRule = activityScenarioRule<OnboardingActivity>()

@Test
fun verifyAllRequiredViewsAreDisplayed() {

    // Create a TestNavHostController
    val navController = TestNavHostController(
        ApplicationProvider.getApplicationContext()
    )

    navController.setGraph(R.navigation.navigation_onboarding)

    // Create a graphical FragmentScenario for the TitleScreen
    val userEmailScenario = launchFragmentInContainer<EmailFragment>()

    // Set the NavController property on the fragment
    userEmailScenario.onFragment { fragment ->
        Navigation.setViewNavController(fragment.requireView(), navController)
    }

    assertThat(navController.currentDestination?.id).isEqualTo(R.id.emailFragment)
}

}`