I'm playing with Dagger right now and apparently some of the features don't work for me.
I'm actually trying to extend my ObjectGraph (via ObjectGraph.plus()
) with mock module which overrides one of the real modules in already created graph. But apparently my mock module is ignored, so real interfaces are called.
However, if I try to provide my mock module during graph creation stage - everything works fine..
In my case MockModule1
overrides providers from RealModule1
Doesn't work:
objectGraph = ObjectGraph.create(new RealModule1(),
new RealModule2(),
new RealModule3());
objectGraph = objectGraph.plus(new MockModule1());
Works fine
objectGraph = ObjectGraph.create(new RealModule1(),
new RealModule2(),
new RealModule3(),
new MockModule1());
RealModule1.java
@Module(injects = MainActivity.class)
public class RealModule1 {
@Provides
ISomething provideSomething() {
return new Something();
}
}
MockModule1.java
@Module(overrides=true, injects = MainActivity.class)
public class MockModule1 {
@Provides
ISomething provideSomething() {
return new MockSomething();
}
}
Am I missing something?