The workaround proposed by @tomrozb is very good and put me on the right track, but my problem with it was that it exposed a setTestComponent()
method in the PRODUCTION Application
class. I was able to get this working slightly differently, such that my production application doesn't have to know anything at all about my testing environment.
TL;DR - Extend your Application class with a test application that uses your test component and module. Then create a custom test runner that runs on the test application instead of your production application.
EDIT: This method only works for global dependencies (typically marked with @Singleton
). If your app has components with different scope (e.g. per activity) then you'll either need to create subclasses for each scope, or use @tomrozb's original answer. Thanks to @tomrozb for pointing this out!
This example uses the AndroidJUnitRunner test runner but this could probably be adapted to Robolectric and others.
First, my production application. It looks something like this:
public class MyApp extends Application {
protected MyComponent component;
public void setComponent() {
component = DaggerMyComponent.builder()
.myModule(new MyModule())
.build();
component.inject(this);
}
public MyComponent getComponent() {
return component;
}
@Override
public void onCreate() {
super.onCreate();
setComponent();
}
}
This way, my activities and other class that use @Inject
simply have to call something like getApp().getComponent().inject(this);
to inject themselves into the dependency graph.
For completeness, here is my component:
@Singleton
@Component(modules = {MyModule.class})
public interface MyComponent {
void inject(MyApp app);
// other injects and getters
}
And my module:
@Module
public class MyModule {
// EDIT: This solution only works for global dependencies
@Provides @Singleton
public MyClass provideMyClass() { ... }
// ... other providers
}
For the testing environment, extend your test component from your production component. This is the same as in @tomrozb's answer.
@Singleton
@Component(modules = {MyTestModule.class})
public interface MyTestComponent extends MyComponent {
// more component methods if necessary
}
And the test module can be whatever you want. Presumably you'll handle your mocking and stuff in here (I use Mockito).
@Module
public class MyTestModule {
// EDIT: This solution only works for global dependencies
@Provides @Singleton
public MyClass provideMyClass() { ... }
// Make sure to implement all the same methods here that are in MyModule,
// even though it's not an override.
}
So now, the tricky part. Create a test application class that extends from your production application class, and override the setComponent()
method to set the test component with the test module. Note that this can only work if MyTestComponent
is a descendant of MyComponent
.
public class MyTestApp extends MyApp {
// Make sure to call this method during setup of your tests!
@Override
public void setComponent() {
component = DaggerMyTestComponent.builder()
.myTestModule(new MyTestModule())
.build();
component.inject(this)
}
}
Make sure you call setComponent()
on the app before you begin your tests to make sure the graph is set up correctly. Something like this:
@Before
public void setUp() {
MyTestApp app = (MyTestApp) getInstrumentation().getTargetContext().getApplicationContext();
app.setComponent()
((MyTestComponent) app.getComponent()).inject(this)
}
Finally, the last missing piece is to override your TestRunner with a custom test runner. In my project I was using the AndroidJUnitRunner
but it looks like you can do the same with Robolectric.
public class TestRunner extends AndroidJUnitRunner {
@Override
public Application newApplication(@NonNull ClassLoader cl, String className, Context context)
throws InstantiationException, IllegalAccessException, ClassNotFoundException {
return super.newApplication(cl, MyTestApp.class.getName(), context);
}
}
You'll also have to update your testInstrumentationRunner
gradle, like so:
testInstrumentationRunner "com.mypackage.TestRunner"
And if you're using Android Studio, you'll also have to click Edit Configuration from the run menu and enter the name of your test runner under "Specific instrumentation runner".
And that's it! Hopefully this information helps somebody :)