I am trying to use Dagger in an Android functional test which inherits ActivityInstrumentationTestCase2.
The setup code looks like this:
@Override
protected void setUp() {
// TODO Auto-generated method stub
try {
super.setUp();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ObjectGraph.create(new TestModule()).inject(this);
this.activity = super.getActivity();
}`
The OnCreate method, which is fired by calling super.getActivity()
, does not use the classes provided by the TestModule. But if I run my activity manually (outside of the testing context) then all the appropriate classes are provided/injected by my non-test module.
Dagger is a fully static, compile-time dependency injection framework for Java, Kotlin, and Android. It is an adaptation of an earlier version created by Square and now maintained by Google.
No, dependency injection is not essential for unit testing.
Dependency injection is a programming technique that makes a class independent of its dependencies. It achieves that by decoupling the usage of an object from its creation. This helps you to follow SOLID's dependency inversion and single responsibility principles, in order to write a good programme.
Dagger constructs instances of your application classes and satisfies their dependencies. It uses the javax. inject. Inject annotation to identify which constructors and fields it is interested in. Use @Inject to annotate the constructor that Dagger should use to create instances of a class.
I found a way to use Dagger with ActivityInstrumentationTestCase2
by lazily creating the Object Graph. What I do is wait to create the Object Graph until the very first time a class wants to be injected, so as long as you add your modules before calling getActivity()
(which starts the activity lifecycle for the activity under test) and use overrides = true
in your test modules, this will work. Here's the relevant classes and snippets:
GraphHolder
, as the name implies, holds the ObjectGraph
object for us. We will make all calls to this class rather than directly to ObjectGraph
.
public class GraphHolder {
private static GraphHolder sInstance;
private Object[] mModules;
private ObjectGraph mGraph;
private GraphHolder() {
}
public static GraphHolder getInstance() {
if (sInstance == null) {
sInstance = new GraphHolder();
}
return sInstance;
}
public void inject(Object object) {
if (mGraph == null) {
create();
}
mGraph.inject(object);
}
public <T> T get(Class<T> type) {
if (mGraph == null) {
create();
}
return mGraph.get(type);
}
public void addModules(Object... modules) {
if (mGraph != null) {
mGraph.plus(modules);
} else {
if (mModules == null) {
mModules = modules;
} else {
mModules = concatenate(mModules, modules);
}
}
}
private void create() {
mGraph = ObjectGraph.create(mModules);
mModules = null;
}
private Object[] concatenate(Object[] a, Object[] b) {
int aLength = a.length;
int bLength = b.length;
Object[] c = new Object[aLength + bLength];
System.arraycopy(a, 0, c, 0, aLength);
System.arraycopy(b, 0, c, aLength, bLength);
return c;
}
}
We'll add our modules in the Application
class:
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
GraphHolder.getInstance().addModules(getModules());
}
Object[] getModules() {
return new Object[]{
// your modules here
};
}
}
Inside the classes we want to inject, we'll simply call GraphHolder.getInstance().inject(this)
rather than ObjectGraph.inject(this)
In our test modules, we'll provide the objects we want to override for testing and add overrides = true
to the @Module
annotation. This tells the object graph to prefer this module's providers over others if there's a conflict.
Then, in our tests:
@Inject Foo mFoo;
@Override
public void setUp() {
super.setUp();
GraphHolder.getInstance().addModules(new TestFooModule());
GraphHolder.getInstance().inject(this); // This is when the object graph will be created
}
ObjectGraph.create(new TestModule()).inject(this);
This code is trying to inject dependencies created by TestModule into your TestCase instead of the tested Activity. What you'd have to do here is
ObjectGraph.create(new TestModule()).inject(this.activity);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With