我正在尝试使用 Android Jetpack 导航组件和片段测试库来测试片段交互。我的应用程序使用 java + Dagger2 作为 DI。
为了测试导航,我创建了一个 JUnit 测试:
@Test
public void testNavigationToLoginFragment() {
// Create a mock NavController
NavController mockNavController = mock(NavController.class);
// Create a graphical FragmentScenario for the Intro Fragment
FragmentScenario<IntroFragment> introFragmentScenario = FragmentScenario.launchInContainer(IntroFragment.class);
// Set the NavController property on the fragment
introFragmentScenario.onFragment(fragment ->
Navigation.setViewNavController(fragment.requireView(), mockNavController)
);
// Verify that performing a click prompts the correct Navigation action
onView(withId(R.id.button_login)).perform(click());
verify(mockNavController).navigate(R.id.action_intro_fragment_to_login_fragment);
}
每当我运行测试时,我都会收到以下错误:
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: No injector factory bound for Class<XXX>
我怎样才能在那里注入我的片段?是否可以将 DaggerFragments 与 一起使用FragmentScenario
?
介绍片段
public class IntroFragment extends DaggerFragment{
@Inject
CreateQuoteRecyclerViewAdapter createQuoteRecyclerViewAdapter;
@Inject
public ViewModelProvider.Factory factory;
@inject
public MyViewModel viewModel;
.....
}
MyViewModel.java
class CreateOrSignInViewModel extends BaseViewModel() {
@Inject
public CreateOrSignInBindingState state;
......
}