Trying to follow the example https://developer.android.com/guide/navigation/navigation-testing, I have an expected error
org.mockito.exceptions.base.MockitoException: Mockito cannot mock this class: class androidx.navigation.NavController.
even after include all the dependencies as here: https://developer.android.com/training/testing/set-up-project
@RunWith(AndroidJUnit4::class)
class MyShopFragmentTest {
@Test
fun testMyShopScenario_F001() {
// Create a mock NavController
val mockNavController = mock(NavController::class.java)
// Create a graphical FragmentScenario for the MyShopFragment
val myShopFragmentScenario = launchFragmentInContainer<MyShopFragment>()
// Set the NavController property on the fragment
myShopFragmentScenario.onFragment { fragment ->
Navigation.setViewNavController(fragment.requireView(), mockNavController)
}
// Verify that performing a click prompts the correct Navigation action
onView(ViewMatchers.withId(R.id.search)).perform(ViewActions.click())
verify(mockNavController).navigate(R.id.searchFragment)
}
}
Any idea?
The solution was use the all-open annotation (https://kotlinlang.org/docs/reference/compiler-plugins.html) and configure the project as described here --> https://developer.android.com/training/testing/set-up-project