Inject your dagger dependencies without modules.
This stuff is in the PoC state. Before you ever think about using it please check this page: https://google.github.io/dagger/testing.html
- Likely that you don't have to use Dagger in your unit tests at all.
- You shouldn't really mock stuff out in your instrumentation/end-to-end/functional/acceptance tests. Use fakes instead.
Dagger2's authors advice to not use it for unit testing. While this is a pretty good advice, mocking Dagger2-driven injections makes sense in some particular cases, like Android Instrumentation Tests. Doug aims to remove the clutter code needed to mock your dependencies using Dagger.
And yeah, you can now mock stuff that's wired using the @Inject
annotation rather than a @Provides
method.
Simple as that.
public class Greeter {
@Inject
Greeter() { }
}
@Inject
Greeter greeter;
@Mock
Greeter greeterMock;
@Test
public void should_be_mocked() {
DaggerComponent component = DaggerComponent.create();
Doug.mock(component, greeterMock);
component.inject(this);
assertThat(greeter).isSameAs(greeterMock);
}