Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Espresso, Dagger2 set ViemodelProvider.Factory on BaseActivity

I have an abstract AccountRequiredActivity that looks like this (and works fine):

public abstract class AccountRequiredActivity extends LifecycleActivity {

    @Inject
    ViewModelProvider.Factory viewModelFactory;

    private AccountViewModel accountViewModel;

    public abstract void doOnCreate(Bundle savedInstanceState);
    public abstract void doOnResume();

    @Override
    protected final void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_loading_app);
        AndroidInjection.inject(this);
        accountViewModel = ViewModelProviders.of(this, viewModelFactory).get(AccountViewModel.class);

        if(!accountViewModel.isAuthenticated()) {
            redirectToLogin();
        } else {
            doOnCreate(savedInstanceState);
        };

    }

    @Override
    protected void onResume() {
        super.onResume();
        if(!accountViewModel.isAuthenticated()) {
            redirectToLogin();
        } else {
            doOnResume();
        };
    }

    private void redirectToLogin() {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

}

The problem during tests is that there is no way for me to set the viewModelFactory on the activity.

When an activity has a fragment, I can just do something like:

@Before
public void init() {
    LoginFragment fragment = LoginFragment.newInstance();
    viewModel = mock(AccountViewModel.class);
    when(viewModel.getAuthenticatedUserResource()).thenReturn(authenticatedUser);

    fragment.viewModelFactory = ViewModelUtil.createFor(viewModel);
    activityRule.getActivity().setFragment(fragment);
}

The problem however in this case is that I use this in my tests (HomeActivity extends AccountRequiredActivity):

@Rule
public ActivityTestRule<HomeActivity> activityTestRule = new ActivityTestRule<>(HomeActivity.class, true, false);

So there is no way to dynamically set the viewModelFactory, as onCreate immediately gets called. There doesn't seem to be a way to get access to the Activity object before onCreate gets called.

How to fix this problem?

Note: I use Dagger 2.11 with AndroidInjector.
Also see this question that I posted yesterday for follow-up info:

Inject ViewModelFactory.Provider in activity for espresso testing

like image 383
html_programmer Avatar asked Jan 31 '26 08:01

html_programmer


1 Answers

I solved the problem by overriding AndroidInjector's inject() method:

@Override
public AndroidInjector<Activity> activityInjector() {
    return new AndroidInjector<Activity>() {
        @Override
        public void inject(Activity instance) {
            AccountViewModel viewModel = mock( AccountViewModel.class );
            if(instance instanceof TestHomeActivity) {
                ((TestHomeActivity) instance).viewModelFactory = ViewModelUtil.createFor( viewModel );
            }
        }
    };
} 
like image 174
html_programmer Avatar answered Feb 01 '26 20:02

html_programmer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!