gpt4 book ai didi

android - 如何使用导航组件( fragment 到 fragment )在 Espresso 中测试 fragment 导航?

转载 作者:行者123 更新时间:2023-12-03 13:33:09 25 4
gpt4 key购买 nike

当我单击一个按钮时,我想测试 fragment 导航(从一个 fragment 到另一个),但在测试中,它永远不会导航到目的地。

当我手动测试它时,它可以正常工作。

我也在使用导航组件。

我研究并找到了来自 Google 的示例代码,但我无法运行它

我的测试:

@RunWith(AndroidJUnit4::class)
@LargeTest
class OnBoardingFragmentTest{

@Test fun navigationTest(){
val scenario = launchFragmentInContainer<OnBoardingFragment>()
onView(withId(R.id.btn_start)).perform(click())
}

在 Fragment 处单击按钮的代码:
btn_start.setOnClickListener { view ->
Navigation.findNavController(view).navigate(R.id.action_onBoardingFragment_to_loginFragment)
}

来自 Google 的示例:

我尝试使用来自 Google 的这个示例来测试 fragment 导航,但我无法运行它,因为无法识别 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 ->
Navigation.setViewNavController(fragment.requireView(), mockNavController)
}

// Verify that performing a click prompts the correct Navigation action
onView(ViewMatchers.withId(R.id.play_btn)).perform(ViewActions.click())
verify(mockNavController).navigate(R.id.action_title_screen_to_in_game)
}

测试时抛出的错误:
androidx.test.espresso.PerformException: Error performing 'single click - At Coordinates: 835, 1565 and precision: 16, 16' on view 'with id: tokencash.com.stx.tokencash:id/btn_start'.
at androidx.test.espresso.PerformException$Builder.build(PerformException.java:86)
at androidx.test.espresso.base.DefaultFailureHandler.getUserFriendlyError(DefaultFailureHandler.java:87)
at androidx.test.espresso.base.DefaultFailureHandler.handle(DefaultFailureHandler.java:59)
at androidx.test.espresso.ViewInteraction.waitForAndHandleInteractionResults(ViewInteraction.java:322)
at androidx.test.espresso.ViewInteraction.desugaredPerform(ViewInteraction.java:178)
at androidx.test.espresso.ViewInteraction.perform(ViewInteraction.java:119)
at tokencash.com.stx.OnBoardingFragmentTest.generalTest(OnBoardingFragmentTest.kt:57)
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 androidx.test.internal.runner.junit4.statement.RunBefores.evaluate(RunBefores.java:80)
at androidx.test.rule.ActivityTestRule$ActivityStatement.evaluate(ActivityTestRule.java:531)
at org.junit.rules.RunRules.evaluate(RunRules.java:20)
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:104)
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:392)
at android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:2145)
Caused by: java.lang.IllegalStateException: View androidx.constraintlayout.widget.ConstraintLayout{3c7882 V.E...... ......ID 0,0-1080,1605} does not have a NavController set
at androidx.navigation.Navigation.findNavController(Navigation.java:84)
at tokencash.com.stx.tokencash.fragments.OnBoardingFragment$setListeners$1.onClick(OnBoardingFragment.kt:53)
at android.view.View.performClick(View.java:6597)
at android.view.View.performClickInternal(View.java:6574)
at android.view.View.access$3100(View.java:778)
at android.view.View$PerformClick.run(View.java:25885)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at androidx.test.espresso.base.Interrogator.loopAndInterrogate(Interrogator.java:148)
at androidx.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:525)
at androidx.test.espresso.base.UiControllerImpl.loopUntil(UiControllerImpl.java:484)
at androidx.test.espresso.base.UiControllerImpl.injectMotionEvent(UiControllerImpl.java:236)
at androidx.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:162)
at androidx.test.espresso.action.MotionEvents.sendUp(MotionEvents.java:139)
at androidx.test.espresso.action.Tap.sendSingleTap(Tap.java:170)
at androidx.test.espresso.action.Tap.access$100(Tap.java:31)
at androidx.test.espresso.action.Tap$1.sendTap(Tap.java:47)
at androidx.test.espresso.action.GeneralClickAction.perform(GeneralClickAction.java:137)
at androidx.test.espresso.ViewInteraction$SingleExecutionViewAction.perform(ViewInteraction.java:366)
at androidx.test.espresso.ViewInteraction.doPerform(ViewInteraction.java:255)
at androidx.test.espresso.ViewInteraction.access$100(ViewInteraction.java:65)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:158)
at androidx.test.espresso.ViewInteraction$1.call(ViewInteraction.java:155)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

我将衷心感谢您的帮助 :)

最佳答案

经过大量测试,我解决了它。

要在测试中使用从一个 fragment 到另一个 fragment 的导航,您应该模拟一个 NavController。

我错误地设置了 Mockito。

设置 Mockito:*

dependencies {
testImplementation 'org.mockito:mockito-core:2.24.5'
androidTestImplementation 'org.mockito:mockito-android:2.24.5'
}

在测试中做静态导入
import org.mockito.Mockito.mock
import org.mockito.Mockito.verify

和简单的复制和粘贴谷歌代码
@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 ->
Navigation.setViewNavController(fragment.requireView(), mockNavController)
}

// Verify that performing a click prompts the correct Navigation action
onView(ViewMatchers.withId(R.id.play_btn)).perform(ViewActions.click())
verify(mockNavController).navigate(R.id.action_title_screen_to_in_game)
}

我希望它可以帮助别人;)

关于android - 如何使用导航组件( fragment 到 fragment )在 Espresso 中测试 fragment 导航?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58896803/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com