尝试模拟导航控制器时出错



尝试按照示例进行操作 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'

效果很好。

解决方案是使用全开注释 (https://kotlinlang.org/docs/reference/compiler-plugins.html( 并按照此处所述配置项目 --> https://developer.android.com/training/testing/set-up-project

相关内容

  • 没有找到相关文章

最新更新