Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Dynamically add test modules in appComponent dagger 2?

Hi is it possible to add test modules in my AppComponent?

Below is my real representation of my appComponent

@Singleton
@Component(modules = arrayOf(MainModule::class,
        AnalyticsModule::class,
        MainAndroidBinding::class,
        AccountAndroidBinding::class,
        AndroidSupportInjectionModule::class,
        HomeAndroidBinding::class,
        NetworkModule::class))
interface ApplicationComponent : AndroidInjector<DaggerApplication> {
    fun inject(myApplication: MyApplication)
    override fun inject(instance: DaggerApplication)

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(applicaton: Application): Builder
        fun build(): ApplicationComponent
    }

}

I could just add the test modules directly to the testAppComponent like this but it doesnt offer me much flexibility to dynamically add different testModules.

@Singleton
@Component(modules = [
    (MainModuleTest::class),
    (TestMainAndroidBindingTest::class),
    (AccountAndroidBindingTest::class),
    (AnalyticsModuleTest::class),
    (AndroidSupportInjectionModule::class),
    (NetworkModuleTest::class)])
interface TestAppComponent : ApplicationComponent {

    fun inject(launchActivityTest: LaunchActivityTest)

    @Component.Builder
    interface Builder {
        @BindsInstance
        fun application(applicaton: Application): Builder

        fun build(): TestAppComponent
    }
}

Here is my MyApplication class

class MyApplication : DaggerApplication() {

    companion object {

        private lateinit var application: MyApplication

        fun get(): MyApplication {
            return application
        }
    }


    @Inject
    lateinit var dispatchingActivityInjector: DispatchingAndroidInjector<Activity>

    lateinit var applicationComponent: ApplicationComponent

    override fun onCreate() {
        super.onCreate()
        application = this
    }

    override fun applicationInjector(): AndroidInjector<out DaggerApplication> {
        applicationComponent = DaggerApplicationComponent.builder()
                .application(this)
                .build()
        applicationComponent.inject(this)
        return applicationComponent
    }

    override fun attachBaseContext(base: Context?) {
        super.attachBaseContext(base)
        MultiDex.install(this)
    }

}

On the LaunchActivityTest this is how i set it up to use this testApp component

  @Before
    fun setUp() {
        val app = InstrumentationRegistry.getTargetContext().applicationContext as MyApplication
        val testAppComponent = DaggerTestAppComponent.builder().application(app).build()
        app.applicationComponent = testAppComponent
        testAppComponent.inject(this)
    }

I was following this guide until i stumbled o the point where my DaggerTestAppComponent doesnt expose the modules for me to dynamically add myself due to the fact that my AppComponent class extends AndroidInjector which automatically adds the modules for you

https://proandroiddev.com/writing-espresso-instrumentation-tests-with-dagger2-kotlin-d30f12c4769b

The above dynamically adds its modules like this:

testAppComponent = DaggerTestAppComponent.builder()
                .appModule(AppModule(app))
                .apiModule(TestApiModule())
                .prefModule(TestPrefModule())
                .build()

I cant do that in my case unless i redo my AppComponent so that it doesnt extend AndroidInjector. If i do that then in my real impl code i have to manually set the modules.

Is there any other way?

like image 474
Jonathan Avatar asked May 29 '18 09:05

Jonathan


1 Answers

You should add a function to your component builder and use "BindsInstance".

Example component

@Singleton
@Component(modules = {
    AndroidSupportInjectionModule.class,
    ApplicationTestModule.class,
    ActivityBuilder.class})
public interface TestExampleComponent extends AndroidInjector<DaggerApplication> {

void inject(TestApplication app);

@Override
void inject(DaggerApplication instance);

@Component.Builder
interface Builder {
    @BindsInstance
    TestExampleComponent.Builder application(DaggerApplication application);
    Builder applicationModule(ApplicationTestModule appTestModule);
    TestExampleComponent build();
  }
}

In this component, I added applicationModule function with using "BindsInstance" and I can pass ApplicationTestModule.

Then you can add different test modules.

TestApplicationComponent appComponent = DaggerTestAppComponent.builder().application(this).
applicationModule(appTestModule).build();
appComponent.inject(this);
like image 190
SavasCinar Avatar answered Sep 29 '22 21:09

SavasCinar