lundi 29 mai 2017

How to mock activity dependencias in an instrumentation test

I want to get a reference to my activity before it is created in order to inject some mocks.

So I have my activity test rule like:

   ActivityTestRule mActivityRule = new ActivityTestRule<MainActivity>(MainActivity,class, true, false)

I know that ActivityTestRule has a beforeActivityLaunched callback, but there I cant get any reference to my activity (is null there).

So if I do:

@Rule
public ActivityTestRule<MainActivity> mActivityTestRule = new ActivityTestRule(
        MainActivity.class) {
    @Override
    protected void beforeActivityLaunched() {
        super.beforeActivityLaunched();
        MainActivity act = (MainActivity)getActivity();
         //Here act is null

I cannot get a reference to my activity in the setUp method of my test class either. Activity reference is ready only when I do rule.launchActivity(), but then Activity is executed and I cannot pass any dependencies before. I know that I can use dagger for that, but I want to avoid it for this case. Is there any way to prepare the activity dependencies without dagger before it is launched?

Aucun commentaire:

Enregistrer un commentaire