19

For 'regular' Java project overriding the dependencies in the unit tests with mock/fake ones is easy. You have to simply build your Dagger component and give it to the 'main' class that drives you application.

For Android things are not that simple and I've searched for a long time for decent example but I was unable to find so I had to created my own implementation and I will really appreciate feedback is this a correct way to use Dagger 2 or there is a simpler/more elegant way to override the dependencies.

Here the explanation (project source can be found on github):

Given we have a simple app that uses Dagger 2 with single dagger component with single module we want to create android unit tests that use JUnit4, Mockito and Espresso:

In the MyApp Application class the component/injector is initialized like this:

public class MyApp extends Application {
    private MyDaggerComponent mInjector;

    public void onCreate() {
        super.onCreate();
        initInjector();
    }

    protected void initInjector() {
        mInjector = DaggerMyDaggerComponent.builder().httpModule(new HttpModule(new OkHttpClient())).build();

        onInjectorInitialized(mInjector);
    }

    private void onInjectorInitialized(MyDaggerComponent inj) {
        inj.inject(this);
    }

    public void externalInjectorInitialization(MyDaggerComponent injector) {
        mInjector = injector;

        onInjectorInitialized(injector);
    }

    ...

In the code above: Normal application start goes trough onCreate() which calls initInjector() which creates the injector and then calls onInjectorInitialized().

The externalInjectorInitialization() method is ment to be called by the unit tests in order to set the injector from external source, i.e. a unit test.

So far, so good.

Let's see how the things on the unit tests side looks:

We need to create MyTestApp calls which extends MyApp class and overrides initInjector with empty method in order to avoid double injector creation (because we will create a new one in our unit test):

public class MyTestApp extends MyApp {
    @Override
    protected void initInjector() {
        // empty
    }
}

Then we have to somehow replace the original MyApp with MyTestApp. This is done via custom test runner:

public class MyTestRunner extends AndroidJUnitRunner {
    @Override
    public Application newApplication(ClassLoader cl,
                                      String className,
                                      Context context) throws InstantiationException,
            IllegalAccessException,
            ClassNotFoundException {


        return super.newApplication(cl, MyTestApp.class.getName(), context);
    }
}

... where in newApplication() we effectively replace the original app class with the test one.

Then we have to tell the testing framework that we have and want to use our custom test runner so in the build.gradle we add:

defaultConfig {
    ...
    testInstrumentationRunner 'com.bolyartech.d2overrides.utils.MyTestRunner'
    ...
}

When a unit test is run our original MyApp is replaced with MyTestApp. Now we have to create and provide our component/injector with mocks/fakes to the app with externalInjectorInitialization(). For that purpose we extends the normal ActivityTestRule:

@Rule
public ActivityTestRule<Act_Main> mActivityRule = new ActivityTestRule<Act_Main>(
        Act_Main.class) {


    @Override
    protected void beforeActivityLaunched() {
        super.beforeActivityLaunched();

        OkHttpClient mockHttp = create mock OkHttpClient

        MyDaggerComponent injector = DaggerMyDaggerComponent.
                builder().httpModule(new HttpModule(mockHttp)).build();

        MyApp app = (MyApp) InstrumentationRegistry.getInstrumentation().
                getTargetContext().getApplicationContext();

        app.externalInjectorInitialization(injector);

    }
};

and then we do our test the usual way:

@Test
public void testHttpRequest() throws IOException {
    onView(withId(R.id.btn_execute)).perform(click());

    onView(withId(R.id.tv_result))
            .check(matches(withText(EXPECTED_RESPONSE_BODY)));
}

Above method for (module) overrides works but it requires creating one test class per each test in order to be able to provide separate rule/(mocks setup) per each test. I suspect/guess/hope that there is a easier and more elegant way. Is there?

This method is largely based on the answer of @tomrozb for this question. I just added the logic to avoid double injector creation.

Community
  • 1
  • 1
Ognyan
  • 13,452
  • 5
  • 64
  • 82

2 Answers2

8

1. Inject over dependencies

Two things to note:

  1. Components can provide themselves
  2. If you can inject it once, you can inject it again (and override the old dependencies)

What I do is just inject from my test case over the old dependencies. Since your code is clean and everything is scoped correctly nothing should go wrong—right?

The following will only work if you don't rely on Global State since changing the app component at runtime will not work if you keep references to the old one at some place. As soon as you create your next Activity it will fetch the new app component and your test dependencies will be provided.

This method depends on correct handling of scopes. Finishing and restarting an activity should recreate its dependencies. You therefore can switch app components when there is no activity running or before starting a new one.

In your testcase just create your component as you need it

// in @Test or @Before, just inject 'over' the old state
App app = (App) InstrumentationRegistry.getTargetContext().getApplicationContext();
AppComponent component = DaggerAppComponent.builder()
        .appModule(new AppModule(app))
        .build();
component.inject(app);

If you have an application like the following...

public class App extends Application {

    @Inject
    AppComponent mComponent;

    @Override
    public void onCreate() {
        super.onCreate();
        DaggerAppComponent.builder().appModule(new AppModule(this)).build().inject(this);
    }
}

...it will inject itself and any other dependencies you have defined in your Application. Any subsequent call will then get the new dependencies.


2. Use a different configuration & Application

You can chose the configuration to be used with your instrumentation test:

android {
...
    testBuildType "staging"
}

Using gradle resource merging this leaves you with the option to use multiple different versions of your App for different build types.

Move your Application class from the main source folder to the debug and release folders. Gradle will compile the right source set depending on the configuration. You then can modify your debug and release version of your app to your needs.

If you do not want to have different Application classes for debug and release, you could make another buildType, used just for your instrumentation tests. The same principle applies: Duplicate the Application class to every source set folder, or you will receive compile errors. Since you would then need to have the same class in the debug and rlease directory, you can make another directory to contain your class used for both debug and release. Then add the directory used to your debug and release source sets.

David Medenjak
  • 33,993
  • 14
  • 106
  • 134
  • I was using your approach but I am afraid that it causes problems when the app has additional initializations in `onCreate()`. The whole reason to create my complicated approach was to avoid double injection of the app. About "Since your code is clean and everything is scoped correctly " I am afraid that this is not my case :-). I have a pile of old apps to migrate to dagger 2 – Ognyan Mar 04 '16 at 07:29
  • @Ognyan added a second option, which might also not be exactly what you are looking for, but for sure somewhat closer :) I feel you. Getting the Application stateless is hard. :/ – David Medenjak Mar 04 '16 at 21:43
0

There is a simpler way to do this, even the Dagger 2 docs mention it but they don't make it very obvious. Here's a snippet from the documentation.

@Provides static Pump providePump(Thermosiphon pump) {
    return pump;
}

The Thermosiphon implements Pump and wherever a Pump is requested Dagger injects a Thermosiphon.

Coming back to your example. You can create a Module with a static boolean data member which allows you to dynamically switch between your real and mock test objects, like so.

@Module
public class HttpModule {

    private static boolean isMockingHttp;

    public HttpModule() {}

    public static boolean mockHttp(boolean isMockingHttp) {
        HttpModule.isMockingHttp = isMockingHttp;
    }

    @Provides
    HttpClient providesHttpClient(OkHttpClient impl, MockHttpClient mockImpl) {
        return HttpModule.isMockingHttp ? mockImpl : impl;
    }

}

HttpClient can be the super class which is extended or an interface which is implemented by OkHttpClient and MockHttpClient. Dagger will automatically construct the required class and inject it's internal dependencies just like Thermosiphon.

To mock your HttpClient, just call HttpModule.mockHttp(true) before your dependencies are injected in your application code.

The benefits to this approach are:

  • No need to create separate test components since the mocks are injected at a module level.
  • Application code remains pristine.
aashreys
  • 923
  • 1
  • 7
  • 11
  • 2
    I am afraid I cannot see the benefit of the approach... The boolean flag have to be raised before the first inject in order to switch to the mock. What is your idea, where and when `mockHttp()` should be called? – Ognyan Mar 12 '16 at 08:29
  • You can call mockHttp() in the beforeActivityLaunched() method to inject mocks into the activity being tested since I'm guessing you must have an Component#inject() call somewhere in your activity code to inject dependencies into it. If you want to inject mocks into your application class then youll have to call the mockHttp() before the application class is created. I've never needed to do that before, but seeing from your example the custom Test runner would be the ideal place for this. – aashreys Mar 12 '16 at 09:10
  • 2
    I see. Your idea is that having `HttpModule` to know that there is a such thing as a `MockHttpClient` it better than creating separate @Component in the tests? Also what happens if there is no `MockHttpClient` class/interface but simply `OkHttpClient mockHttp = mock(OkHttpClient.class);` or there are more than one different mock http classes? – Ognyan Mar 12 '16 at 10:16
  • If you want to choose between multiple mock implementations you can set a static int or enum in place of a boolean in your module and then use a switch case in your provides method to return the appropriate implementation. Anyway, the point is that this approach remains flexible while saving you the time and complexity of having to manage different components and swapping them out at runtime within your app. I'm sure you can get creative with it once you implement it. – aashreys Mar 12 '16 at 11:13
  • You'll need to create an interface (or superclass) if you want this to work since only then will Dagger be able to return both your real and mock implementations from the same method. It's standard OOP polymorphism at work here. I've not had much experience with Mockito and `OkHttpClient mockHttp = mock(OkHttpClient.class);` style mocking before however Dagger should be able to return anything that extends or is an `HttpClient` from the `providesHttpClient()` method, so you can try returning the mock object from there. – aashreys Mar 12 '16 at 12:03