Reputation: 5487
I want to test fragment navigations, which is handled by the MainActivity.
for that I have created HomeFragmentTest
class
@MediumTest
@RunWith(AndroidJUnit4::class)
class HomeFragmenTest {
@Rule
@JvmField
val mockitoRule: MockitoRule = MockitoJUnit.rule()
@Test
fun onUsersButtonClick_navigateUsersListFragmet() {
val navController = mock(NavController::class.java)
val fragScenario = launchFragmentInContainer<UsersListFragment>()
fragScenario.onFragment {
Navigation.setViewNavController(it.requireView(), navController)
}
onView(withId(R.id.usersButton))
.perform(click())
verify(navController).navigate(R.id.usersListFragment)
}
}
running the test invokes button click action and then viewModel
invokes navigate method which will post event to the MainActivity
. MainActivity observes this event and calls navController
navigate method
viewModel.navAction.observe(this, { direction ->
navController?.navigate(direction)
})
The problem is that my test is failing
Wanted but not invoked:
navController.navigate(2131296591);
Actually, there were zero interactions with this mock.
Upvotes: 0
Views: 143