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.
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() .
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With