Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dagger 2 How to inject object into test

I would like to use my realm manager into unit test module. I did

@Singleton
@Component(modules = {
        TestApplicationModule.class,
        AndroidSupportInjectionModule.class,
        TestStoreDataModule.class,
        TestUtilsModule.class})
public interface AppComponentTest extends AppComponent {

    @Component.Builder
    interface Builder {

        @BindsInstance
        AppComponentTest.Builder application(Application application);

        AppComponentTest build();
    }
}

and then I want to achieve

@RunWith(RobolectricTestRunner.class)
@Config(application = TestVerioriApplication.class, sdk=27)
public class BaseVerificationQuestionnaireFragmentTest {

    @Inject
    RealmManager realmManager;
}

But realmManager is null. How to use dagger 2 to write simple module test ? I used dagger-mock but it does not help. My module contains

@Module(includes = StoreDataModule.class)
public class TestStoreDataModule {

    @Provides
    @Singleton
    public static RealmConfiguration provideRealmConfiguration(RealmConstants realmConstants) {
        return new RealmConfiguration.Builder()
                .name(realmConstants.getName())
                .encryptionKey("Implement this key".getBytes())
                .schemaVersion(realmConstants.getSchemaVersion())
                .build();
    }

    @Provides
    @Singleton
    public static RealmManager provideRealmManager(RealmConfiguration realmConfiguration, SchedulerProvider schedulerProvider) {
        return new RealmManager(realmConfiguration, schedulerProvider);
    }

}

I tried everything from google, but I don't know how to inject object from graph.

like image 747
Piotr Badura Avatar asked Nov 02 '18 11:11

Piotr Badura


People also ask

How do you inject a Dagger in fragment?

onCreate() , an activity attaches fragments that might want to access activity bindings. When using fragments, inject Dagger in the fragment's onAttach() method. In this case, it can be done before or after calling super. onAttach() .

What is @inject in Dagger?

Dependency Injection, or DI in short, is a design pattern that allows to delegate the creation of objects and their dependencies to another object or framework. It is gaining a lot of interest in Android application development.

How do you inject value at runtime in Dagger?

Inject values at runtime with UI in Dagger2:pureMathModule("Book Name") . build() . inject(this); The difference between DaggerComponent create() and in build() is - create() works when no runtime argument is passed into the constructor, else we use build() method.

How does a Dagger injection work?

Dagger automatically generates code that mimics the code you would otherwise have hand-written. Because the code is generated at compile time, it's traceable and more performant than other reflection-based solutions such as Guice. Note: Use Hilt for dependency injection on Android.


1 Answers

Override your Application class, where you will replace dagger component instance by your TestComponent. Then create your own test runner by overriding AndroidJUnitRunner class where you need to add test application:

class TestRunner : AndroidJUnitRunner() {
   @Throws(InstantiationException::class,IllegalAccessException::class,ClassNotFoundException::class)
 override fun newApplication(cl:ClassLoader,className:String, context:Context):Application {
        return super.newApplication(cl, TestApplication::class.java.name, context)
  }
}

Next register your runner in build.gradle file :

testInstrumentationRunner "com.test.YourTestRunner"

Now you can just replace the implementation of a module that you want to change in test in your test component.

like image 158
Karol Kulbaka Avatar answered Sep 29 '22 15:09

Karol Kulbaka