我想在单击按钮时测试片段导航(从一个片段到另一个),但在测试中,它永远不会导航到目的地。
当我手动测试它时,它可以正常工作。
我也在使用导航组件。
我研究并找到了来自 Google 的示例代码,但我无法运行它
我的测试:
@RunWith(AndroidJUnit4::class)
@LargeTest
class OnBoardingFragmentTest{
@Test fun navigationTest(){
val scenario = launchFragmentInContainer<OnBoardingFragment>()
onView(withId(R.id.btn_start)).perform(click())
}
Run Code Online (Sandbox Code Playgroud)
按钮单击 Fragment 的代码:
btn_start.setOnClickListener { view ->
Navigation.findNavController(view).navigate(R.id.action_onBoardingFragment_to_loginFragment)
}
Run Code Online (Sandbox Code Playgroud)
来自 Google 的示例:
我尝试使用来自 Google 的此示例来测试片段导航,但我无法运行它,因为无法识别 mockito(也尝试使用用于 kotlin 的 mockk 库)。
@RunWith(AndroidJUnit4::class)
class TitleScreenTest {
@Test
fun testNavigationToInGameScreen() {
// Create a mock NavController
val mockNavController = mock(NavController::class.java)
// Create a graphical FragmentScenario for the TitleScreen
val titleScenario = launchFragmentInContainer<TitleScreen>()
// Set the NavController property on the fragment
titleScenario.onFragment { fragment …Run Code Online (Sandbox Code Playgroud)