Joan Colmenero
05/20/2020, 10:34 PMstreetsofboston
05/20/2020, 11:02 PMApplicationComponent
that takes a bunch of inputs in its builder (factory)streetsofboston
05/20/2020, 11:04 PMstreetsofboston
05/20/2020, 11:04 PMopen class MyApp: Application()
builds this DaggerApplicationComponent
and provides it with these inputs for the ‘real’ world, interface-implementations of actual networking, actual bluetooth, actual base-urls for network-requests.streetsofboston
05/20/2020, 11:07 PMclass MyTestableApp : MyApp()
and this one builds the same DaggerApplicationComponent
but with a different set of inputs. Maybe with fake implementations of hardware/network, maybe with the a base-url of “http://localhost:8888” instead of one of an actual server.streetsofboston
05/20/2020, 11:07 PMMyTestablApp
instead of MyApp
streetsofboston
05/20/2020, 11:12 PMDaggerApplicationComponent
but have a factory that takes input and this input is implemented different on the actual app than on the testable app. I don’t use a (Dagger)TestAppComponent
streetsofboston
05/20/2020, 11:25 PMopen class MyApp: Application() {
...
override fun onCreate() {
...
DaggerApplicationComponent
.builder()
.applicationContext(this)
.authServiceConfig(createAuthServiceConfigModule())
.restServiceConfig(createRestServiceConfigModule())
.build()
.inject(this)
}
protected val createAuthServiceConfigModule() : AuthServiceConfigModule = authServiceConfigModule
protected val createRestServiceConfigModule() : RestServiceConfigModule = restServiceConfigModule
...
}
// AuthServiceConfigModule is a @Module
private val authServiceConfigModule = AuthServiceConfigModule(
rootUrl = BuildConfig.ROOT_URL,
clientId = BuildConfig.CLIENT_ID,
scope = BuildConfig.SCOPE,
redirectUrl = BuildConfig.REDIRECT_URL
)
// RestServiceConfigModule is a @Module
private val restServiceConfigModule = RestServiceConfigModule(
baseUrlFiberServices = BuildConfig.BASE_URL,
baseUrlForAwsBucket = BuildConfig.AWS_URL,
)
and override the methods createAuthServiceConfigModule
and createRestServiceConfigModule
in MyTestableApp
which will provide `@Module`s AuthServiceConfigModule
and RestServiceConfigModule
with local-host base-urls and such.Joan Colmenero
05/21/2020, 7:59 AMstreetsofboston
05/21/2020, 12:12 PMRetroFitModule
provides the OkHttpClient dependency and depending what type of dependant receives it (through constructor injection or field injection) you won't need or will need to manually inject that dependant.Joan Colmenero
05/21/2020, 1:10 PM@Inject lateinit var retrofit: Retrofit
is not injected... I guess is injected, but I can not get it from the test.streetsofboston
05/21/2020, 1:19 PM@Module
class RetrofitModule(private val baseUrl: String) {
@Singleton
@Provides
fun provideOkHttpClient(): OkHttpClient {
return OkHttpClient.Builder(). ... ... .build()
}
@Singleton
@Provides
fun provideRetrofit(client: OkHttpClient) {
return Retrofit.Builer().baseUrl(baseUrl) .. ... .build()
}
...
}
Then the actual injection depends on what the target is.
If it is field injection, you’d have to do inject(target)
, when you have @Inject lateinit var….
.
For constructor injection class MyTarget @Inject constructor(private val ….)
, then you don’t need to ‘manually’ inject.streetsofboston
05/21/2020, 1:23 PMJoan Colmenero
05/21/2020, 1:26 PMAndroidInjector.inject(this)
and then I can use @Inject lateinit var presenter : MyContract.Presenter
so I guess I'm missing something like AndroidInjector.inject(this)
somewhere, but don't know where to inject this TestAppComponent...streetsofboston
05/21/2020, 1:30 PMcreate(….)
method on those factories.streetsofboston
05/21/2020, 1:32 PMclass MyPresenter private constructor(private val reftrofit: Retrofit) {
class Factory @Inject constructor(private val retrofit: Retrofit) {
fun create(): MyPresenter = MyPresenter(retrofit)
}
...
}
streetsofboston
05/21/2020, 1:33 PMMyPresenter.Factory
in a separate Dagger-Module.Joan Colmenero
05/21/2020, 1:35 PMAndroidInjector.inject(this)
for the tests?streetsofboston
05/21/2020, 1:47 PMstreetsofboston
05/21/2020, 1:49 PMJoan Colmenero
05/21/2020, 2:22 PMstreetsofboston
05/21/2020, 2:28 PMAndroidInject
work for tests. You test would be your ‘activity’. I wonder if it is possible to extract something common from your activities and have that be extended by your activities and your tests that would make dagger-android/android-inject work…Joan Colmenero
05/21/2020, 2:33 PMstreetsofboston
05/21/2020, 2:35 PMJoan Colmenero
05/21/2020, 2:51 PMstreetsofboston
05/21/2020, 2:55 PM