The Dagger 2 documentation offers various configurations for testing and production using the interface for ProductionComponent and TestComponent , as shown below
@Component(modules = { OAuthModule.class, // real auth FooServiceModule.class, // real backend OtherApplicationModule.class, /* … */ }) interface ProductionComponent { Server server(); } @Component(modules = { FakeAuthModule.class, // fake auth FakeFooServiceModule.class, // fake backend OtherApplicationModule.class, /* … */}) interface TestComponent extends ProductionComponent { FakeAuthManager fakeAuthManager(); FakeFooService fakeFooService(); }
Let's say we have an Android activity ( MyApp ) that uses a ProductionComponent :
public class MyApp extends Application { private ProductionComponent component; @Override public void onCreate() { super.onCreate(); component = ProductionComponent.builder() .serverModule(new ServerModule()) .build(); } }
Generally, what is the best way to use DaggerTestComponent.builder() instead of ProductionComponent.builder() in Android integration tests?
I'm not sure how to use fakes; Should I do a new activity in /androidTest that extends MyApp ? Or do I need to pass the new DaggerTestComponent to MyApp using getter / setter when I install my test?
source share