问题
Related to this question: Espresso, Dagger2 set ViemodelProvider.Factory on BaseActivity
I went through hell and back to get a ViewModelFactory.Provider on an Activity during my tests, in order to get espresso tests working regarding Android Architecture Components. I expected it to be simple, but I guess it's not...
The example to get it working with fragments is straightforward:
https://github.com/googlesamples/android-architecture-components/blob/master/GithubBrowserSample/app/src/androidTest/java/com/android/example/github/ui/user/UserFragmentTest.java
@Before
public void init() {
UserFragment fragment = UserFragment.create("foo");
viewModel = mock(UserViewModel.class);
when(viewModel.getUser()).thenReturn(userData);
when(viewModel.getRepositories()).thenReturn(repoListData);
navigationController = mock(NavigationController.class);
fragmentBindingAdapters = mock(FragmentBindingAdapters.class);
fragment.viewModelFactory = ViewModelUtil.createFor(viewModel);
fragment.navigationController = navigationController;
fragment.dataBindingComponent = () -> fragmentBindingAdapters;
activityRule.getActivity().setFragment(fragment);
}
However, this simply won't work with activities as I can't get the dependencies on the activity before its creation using an ActivityTestRule.
I followed the same new dependency injection flow with Dagger2 as in the example above using the HasActivityInjector interface.
I would appreciate your help!
回答1:
It’s possible to set injected activity attribute by registering a custom ActivityLifecycleCallbacks in your TestApp in the @Before Method.
Example:
@Before
public void init(){
UserFragment fragment = UserFragment.create("foo");
viewModel = mock(UserViewModel.class);
when(viewModel.getUser()).thenReturn(userData);
when(viewModel.getRepositories()).thenReturn(repoListData);
navigationController = mock(NavigationController.class);
TestApp testApp = ((TestApp) InstrumentationRegistry.getContext().getApplicationContext());
testApp.registerActivityLifecycleCallbacks(new Application.ActivityLifecycleCallbacks() {
@Override
public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
//will be called before the onCreate method of your activity
activity.setViewModelFactory(ViewModelUtil.createFor(viewModel));
}
@Override
public void onActivityStarted(Activity activity) {
}
@Override
public void onActivityResumed(Activity activity) {
}
@Override
public void onActivityPaused(Activity activity) {
}
@Override
public void onActivityStopped(Activity activity) {
}
@Override
public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
}
@Override
public void onActivityDestroyed(Activity activity) {
}
});
fragment.viewModelFactory = ViewModelUtil.createFor(viewModel);
fragment.navigationController = navigationController;
fragment.dataBindingComponent = () -> fragmentBindingAdapters;
activityRule.getActivity().setFragment(fragment);
}
来源:https://stackoverflow.com/questions/46533989/inject-viewmodelfactory-provider-in-activity-for-espresso-testing