Does anyone know how I can use Dagger 2 to inject ...
# android
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.
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?
In an ideal world I would specify an ApiModule like: @Module class ApiModule { @Provides @Singleton fun provideAuthenticationApi(): AuthenticationApi = AuthenticationApiImpl () }
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 ()
@Inject lateinit var Auth: AuthenticationApi