Injection Test Module with Dagger2

I am using Dagger2 in my android application. Basically I embed an HttpClient (interface) in MainActivity .

 @Module public class MainActivityModule{ @Provides public HttpClient providesHttpComponent(){ return new RealHttpClient(); } } @Component( modules = MainActivityModule.class ) public interface MainActivityComponent { public MainActivity injectActivity(MainActivity); } public class MainActivity extends Activity { public void onCreate(Bundle saved){ super.onCreate(); injectDependencies(); } protected void injectDependencies(){ Dagger_MainActivityComponent .builder() .mainActivityComponent( new MainActivityModule()) .build() .injectActivity(this); } } 

It is still so good that it works as expected. Now I want to write some unit tests (not hardware tests for Android) for MainActivity , where I want to use TestMainActivityModule instead of MainActivityModule .

 @Module (overrides = true ) public class TestMainActivtiyModule extends MainActivityModule { @Provides public HttpClient(){ return new MockHttpClient(); } } 

My question is: How to get MainActivity use TestMainActivitiyModule instead of MainActivityModule ? Is there a good solution for this?

My current approach is to use inheritance and override getModule() , something like this

 public class TestMainActivity extend MainActivity { @Override protected void injectDependencies(){ Dagger_MainActivityComponent .builder() .mainActivityComponent( new TestMainActivtiyModule()) .build() .injectActivity(this); } } 

and run unit test instead of TestMainActivity instead of MainActivity .

I think this works, but one of the problems I encounter with this approach is that I cannot run TestMainActivity with Intent , because I cannot specify it in AndroidManifest.xml

Does anyone know a better approach for unit testing with dagger2 on Android?

+5
source share
2 answers

The approach that I started using included support for two modules (one for the application, one for testing) in parallel build options (for example: app and integration ). Still not sure how well this solution scales so well with YMMV. I would be very happy to see a better solution!

It is also wonderful to read: http://engineering.circle.com/instrumentation-testing-with-dagger-mockito-and-espresso/

0
source

I really suggest you check out this boilerplate , as it is completely DI based using Dagger2. It also shows how you can replace your dependencies in a test environment in a very neat way.

The dependencies currently being processed by the boiler stove are as follows:

  • Database Dependency: Encapsulates all database operations.
  • General preference relation: refers to general preferences.
  • Local file dependency: this applies to saving files.
  • Google Analytics Dependency: covers the entire work of event reporting in your analytic backend (GA, segment, FB, Flurry ..)
  • Registration Dependency: Encapsulates all operations related to logging to the console.
  • Api Dependency: Encapsulates All API Related Operations

The power of dependency injection is really convenient, especially for testing, since you can easily switch your dependencies in the test environment to dummy dependencies.

0
source

Source: https://habr.com/ru/post/1216105/


All Articles