Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android plain Junit with Dagger 2

I used to work in MVP and I usually test my presenters using a plain Junit (Not the Instrumentation !) , since Presenters only have the business logic and no references to Android internals whatsoever.

Now by switching to Dagger 2 , I understood that I have a problem setting up a "TestModule" for my app component.

  1. Creating a component will not work from within a test class (probably because "apt" is not running there)
  2. Didn't find any examples for using Dagger with a standard Junit testing. Every example I have found only relies on Instrumentation testing or Roboelectric (which basically mocks Activities and other Android related stuff) , but this is just a UI testing for me , and I don't need that.

Just to make things clear , I am talking about the tests that are located at app->src->test folder not the app->src->androidTest !

So do I do something wrong ? Or missing something ? Can anyone explain or give examples on how to use Dagger 2 in normal unit tests ?

like image 230
Ivelius Avatar asked Aug 19 '16 13:08

Ivelius


People also ask

What is the use of dagger in Android?

For more information about this, check out the Using Dagger in your Android app codelab. Dagger modules are a way to encapsulate how to provide objects in a semantic way. You can include modules in components but you can also include modules inside other modules. This is powerful, but can be easily misused.

What is Google’s “dagger hilt” for Android?

Dagger Hilt is Google’s new opinionated add-on for setting up Dagger 2 for Android. Through its tie-ins with androidx and preconfigured components, it should be able to meet the common dependency injection demands of most apps. Google also lays out a testing philosophy that Hilt can help realize, using the real Dagger graph in tests.

Which annotations are used in Dagger 2?

Dagger 2 Dagger 2 is a compile-time android dependency injection framework that uses Java Specification Request 330 and Annotations. Some of the basic annotations that are used in dagger 2 are: @Module This annotation is used over the class which is used to construct objects and provide the dependencies.

What is the best way to implement dagger dependency injection in Android?

Hilt is built on top of Dagger and it provides a standard way to incorporate Dagger dependency injection into an Android application. Note: If you're already familiar with Dagger, check out these best practices.


1 Answers

I'm not sure if my solution will work for you but I see no reason it shouldn't. First I created testInjectionComponent

@Singleton
@Component(modules = {MockNetworkModule.class})
public interface MockInjectionComponent extends InjectionComponent {
void inject(DaggerUnitTest daggerUnitTest);
}

Then my Unit Tests I add injection in the before method. like so:

@Before
public void setUp() throws Exception {
    MockInjectionComponent mockInjectionComponent = DaggerMockInjectionComponent
            .builder()
            .mockNetworkModule(new MockNetworkModule())
            .build();
    mockInjectionComponent.inject(this);
}

Then I just Annotate my Injected Object.

EDIT : Do not forget to add testApt "com.google.dagger:dagger-compiler:$daggerVersion" at your app.gradle file .

like image 113
Danny Beaumont Avatar answered Oct 19 '22 08:10

Danny Beaumont