Testing with dagger 2 using separate component configurations in Android

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?

+6
source share
1 answer

How about using robolectric with Mockito ?

You can make JUnit test codes using @Test without AndroidTest. I think you are making a test dagger application with Mockito.

links here

http://alexzh.com/tutorials/android-testing-mockito-robolectric/ http://sdudzin.blogspot.kr/2011/01/easy-unit-testing-for-android.html

0
source

All Articles