Does anyone know how I can use Dagger 2 to inject dependencies for an object? I'm looking to do something like:
data API {
@Inject
val Auth: AuthenticationApi
}
Then have AuthenticationApiImpl for the actual implementation. Then in testing be able to provide a new dependency that injects MockAuthenticationApiImpl instead. I can't send to get an ApiModule working for the injection.
a
alex.hart
05/24/2017, 4:34 PM
yperess:
Have you written a proper component capable of injecting an API object? Are you looking to do manual injection on this object? Or are you looking for dagger to do the work for you?
y
yperess
05/24/2017, 4:40 PM
In an ideal world I would specify an ApiModule like:
@Module
class ApiModule {
@Provides @Singleton
fun provideAuthenticationApi(): AuthenticationApi = AuthenticationApiImpl ()
}
yperess
05/24/2017, 4:41 PM
Then I'd have the Auth field populated with an instance of AuthenticationApi via AuthenticationApiImpl. For testing I'd use MockApiModule that would look similar but would return MockAuthenticationApiImpl ()