I am trying to test fragment interactions using the Android Jetpack Navigation Component and the fragment-testing library. My app is using java + Dagger2 as DI.
To test the navigation I have created a JUnit test:
@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);
}
Whenever I run the test I get following error:
java.util.concurrent.ExecutionException: java.lang.IllegalArgumentException: No injector factory bound for Class<XXX>
How can I inject my fragment there? Is it possible to use DaggerFragments with FragmentScenario
?
IntroFragment
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;
......
}