问题
I need to run Android instrumented test on StarterActivity
. Here is how it goes
public class StarterActivity extends BaseActivity<ActivityStarterBinding> {
@Inject
protected StarterViewModel starterViewModel;
@Override
public int getContentView() {
return R.layout.activity_starter;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getApplicationComponent().inject(this);
}
//...
}
And I call for starterViewModel
method in onStart.
StarterViewModel
is injected via a constructor:
public class StarterViewModel {
private final AuthDataModel authDataModel;
@Inject
public StarterViewModel(AuthDataModel authDataModel) {
this.authDataModel = authDataModel;
}
@NonNull
public Single<Boolean> isUserLoggedIn() {
return authDataModel.isUserLoggedIn();
}
}
I found this really nice approach Android testing using Dagger 2, Mockito and a custom JUnit rule. But it needs me to add @Provide method. And application component is going to become "God component" with dependencies on a bunch of Modules (or one "God module").
How can I mock in Espresso test without adding @Provide method and overriding it in tests?
来源:https://stackoverflow.com/questions/45166990/mock-injected-viewmodel