I have an abstract AccountRequiredActivity that looks like this (and works fine):
public abstract class AccountRequiredActivity extends LifecycleActivity {
you can create own testrule
public class MyCustomRule extends ActivityTestRule {
public MyCustomRule(Class activityClass) {
super(activityClass);
}
@Override
protected void beforeActivityLaunched() {
super.beforeActivityLaunched();
// Maybe prepare some mock service calls
// Maybe override some depency injection modules with mocks
}
@Override
protected Intent getActivityIntent() {
Intent customIntent = new Intent();
// add some custom extras and stuff
return customIntent;
}
@Override
protected void afterActivityLaunched() {
super.afterActivityLaunched();
// maybe you want to do something here
}
@Override
protected void afterActivityFinished() {
super.afterActivityFinished();
// Clean up mocks
}
}
and set as ActivityTestRule
@Rule
public ActivityTestRule testRule = new MyCustomRule<>(MainActivity.class);
in beforeActivityLaunched()
you can then inject your viewModelFactory
more here