Android espresso - 如何使用dagger2注入依赖项

j2e*_*nue 3 android dependency-injection android-espresso mockwebserver dagger-2

使用dagger2将依赖注入espresso测试是我想要做的.

我想要一种能够使用dagger为我的测试用例提供依赖关系的方法.

特别是有一个MockwebServer类,我想用匕首注入.这是怎么做到的?我的项目已经设置了匕首.它现在是一个单独的组件,单个组件有5个模块,如下所示:

@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);
}
Run Code Online (Sandbox Code Playgroud)

它的工作很精细.但是现在当我移动到androidTest文件夹进行espresso测试时,我将如何使用以下组件:

    //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);
    }
Run Code Online (Sandbox Code Playgroud)

我一直在做什么将AppTestComponent保留在主要源代码中,但是它不能以这种方式看到MYTESTCLASS?

我想要注入我的类的原因是,我希望在将它传递给改造之后注入一个mockWebServer类,就像这样的baseurl:

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();
    }
    //....
    }
Run Code Online (Sandbox Code Playgroud)

这样我就可以获得MockWebServer的参考,并在我的测试中使用它,并对其进行改造,以便我可以进行快速集成测试

同样在gradle我使用以下依赖项请确认:

 compile 'com.google.dagger:dagger:2.9'
testCompile 'com.google.dagger:dagger:2.9'
annotationProcessor 'com.google.dagger:dagger-compiler:2.9'
Run Code Online (Sandbox Code Playgroud)

小智 9

我将尝试解释它是如何做到的:

在您的应用程序类中,您应该指定包含API服务的Application组件:

protected AppComponent initializeAppComponent() {
    return DaggerAppComponent.builder()
            .apiServiceModule(new APIServiceModule(this))
            .otherModules(new otherModules(this))
            .build();
}
Run Code Online (Sandbox Code Playgroud)

并注入这样:

@Override
public void onCreate() {
    applicationComponent = initializeAppComponent();
    applicationComponent.inject(this)}};
Run Code Online (Sandbox Code Playgroud)

它是标准初始化.您只需将组件构建器移动到稍后可以覆盖的方法.

在你的Android测试包中:

现在,您需要创建新的Application类,它扩展了您的应用程序类,您可以在其中初始化dagger组件.

所以现在你可以覆盖initializeAppComponent()方法并APIServiceModule通过扩展前一个模块的新方法切换你的方法.它应该是这样的:

public class MockApp extends App {

@Override
protected AppComponent initializeAppComponent() {
    return DaggerAppComponent.builder()
            .apiServiceModule(new MockAPIServiceModule(this))
            .otherModules(new OtherModules(this))
            .build();
}

@Module
private class MockAPIServiceModule extends APIServiceModule {

    @Override
    public ApiService provideApiService(@Nonnull final Retrofit retrofit,
                                        @Nonnull final Gson gson) {
        return new ApiService() {
            @Override
            public Observable<LoginResponse> login(@Body final LoginRequest loginRequest) {
                return // what you want to
            }
        }
    }
}}
Run Code Online (Sandbox Code Playgroud)

您需要声明Application应该使用哪个类进行测试.现在你还需要做两件事:1.创建指向新App类的新跑步者

public class MockTestRunner extends AndroidJUnitRunner{

@Override
public void onCreate(Bundle arguments) {
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().permitAll().build());
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().build());
    super.onCreate(arguments);
}
@Override
public Application newApplication(ClassLoader cl, String className, Context context)
        throws InstantiationException, IllegalAccessException, ClassNotFoundException {
    return super.newApplication(cl, MockApp.class.getName(), context);
}}
Run Code Online (Sandbox Code Playgroud)
  1. 声明将使用哪个跑步者build.gradle.

testInstrumentationRunner ".MockTestRunner"

而已.现在您的请求将使用模拟响应!如果您有任何疑问,请询问.

干杯