gpt4 book ai didi

android - 尝试模拟 NavController 时出错

转载 作者:行者123 更新时间:2023-12-04 13:01:17 26 4
gpt4 key购买 nike

试图效仿https://developer.android.com/guide/navigation/navigation-testing ,我有一个预期的错误

org.mockito.exceptions.base.MockitoException:
Mockito 无法模拟此类:类 androidx.navigation.NavController。


即使在包含所有依赖项之后: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)
}
}

任何想法?

最佳答案

对我来说,改变我的依赖

androidTestImplementation 'org.mockito:mockito-core:3.2.4'


androidTestImplementation 'org.mockito:mockito-android:3.2.4'

运作良好。

关于android - 尝试模拟 NavController 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56635092/

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