Injecting dependencies into espresso test using dagger2 is what i would like to do.
I would like a way to be able to provide dependencies for my test cases using dagger.
In particular there is a MockwebServer class i want to inject with dagger. How is this done? My project already has dagger set up. its a single component for now and the single componeent has 5 modules which look like this:
@Singleton
@Component(modules = {AppModule.class, NetworkModule.class, RepositoryModule.class, UseCaseModule.class, ActivityModule.class, PresenterModule.class})
public interface AppComponent {
void inject(NetworkSessionManager target);
void inject(SplashActivity target);
void inject(AuthenticationActivity target);
void inject(WelcomeActivity target);
void inject(LoginFragment target);
}
AND IT WORKS FINE. But now when i move to the androidTest folder to do an espresso test how would i use the following component:
//note the NetworkTestModule.class i want to use is defined instead of //networkModule.class
@Singleton
@Component(modules = {AppModule.class, NetworkTestModule.class, RepositoryModule.class, UseCaseModule.class, ActivityModule.class, PresenterModule.class})
public interface AppTestComponent
{
void inject(NetworkSessionManager target);
void inject(SplashActivity target);
void inject(AuthenticationActivity target);
void inject(WelcomeActivity target);
void inject(LoginFragment target);
void inject (MYTESTCLASS target);
}
what i have been doing it keeping the AppTestComponent in the main source code but it cant see MYTESTCLASS this way ?
The reason i want to inject into my class, is that i want to inject a mockWebServer class after passing it to retrofit as the baseurl like this:
TestNetworkModule.java:
@Provides
@Singleton
public Retrofit provideRetrofit(Converter.Factory converter, OkHttpClient client, @Named(BASE_URL) String baseUrl, MockWebServer server) {
return new Retrofit.Builder()
.baseUrl(server.url("/").toString())
.client(client)
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.addConverterFactory(converter)
.build();
}
@Singleton
@Provides
MockWebServer providerMockWebServer() {
return new MockWebServer();
}
//....
}
this way i can get a reference of the MockWebServer and use it in my tests and have retrofit work with it so i can do fast integration tests
also in gradle i am using the following dependencies please confirm:
compile 'com.google.dagger:dagger:2.9'
testCompile 'com.google.dagger:dagger:2.9'
annotationProcessor 'com.google.dagger:dagger-compiler:2.9'