MockWebServer带有Retrofit和Dagger

时间:2017-03-15 15:32:08

标签: android testing retrofit2 dagger mockwebserver

我试图为我的android项目实现检测测试。在我的主项目中,我使用了匕首和改装,我从我的模块中进行了改造。

这是我提供改造的模块:

@Module
public class NetModule {

    @Provides
    @Singleton
    OkHttpClient.Builder provideOkHttpClientBuilder(){
        OkHttpClient.Builder okHttp = new OkHttpClient.Builder();

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        NoCacheInterceptor noCacheInterceptor = new NoCacheInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        if (!okHttp.interceptors().contains(interceptor))
            okHttp.addInterceptor(interceptor);

        if (!okHttp.interceptors().contains(noCacheInterceptor))
            okHttp.addInterceptor(noCacheInterceptor);

        return okHttp;
    }

    @Provides
    Retrofit provideRetrofit(OkHttpClient.Builder httpClient){
        return new Retrofit.Builder()
              .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
              .addConverterFactory(GsonConverterFactory.create())
              .baseUrl(Constant.BASE_URL)
              .client(httpClient.build()).build();
    }

}

然后,在我的androidTest文件夹中,我创建了TestModule来提供MockWebServer的改造,如下所示:

@Module
public class TestNetModule {
    private final MockWebServer mockWebServer;
    public TestNetModule() {
        mockWebServer = new MockWebServer();
    }

    @Provides
    @Singleton
    MockWebServer providerMockWebServer() {
        return new MockWebServer();
    }

    @Provides
    @Singleton
    OkHttpClient.Builder provideOkHttpClientBuilder(){
        OkHttpClient.Builder okHttp = new OkHttpClient.Builder();

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        NoCacheInterceptor noCacheInterceptor = new NoCacheInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);

        if (!okHttp.interceptors().contains(interceptor))
            okHttp.addInterceptor(interceptor);

        if (!okHttp.interceptors().contains(noCacheInterceptor))
            okHttp.addInterceptor(noCacheInterceptor);

        return okHttp;
    }

    @Provides
    Retrofit.Builder provideRetrofitBuilder(OkHttpClient.Builder httpClient, MockWebServer mockWebServer){
        return new Retrofit.Builder()
              .addCallAdapterFactory(RxJavaCallAdapterFactory.create())
              .addConverterFactory(GsonConverterFactory.create())
              .baseUrl(mockWebServer.url("/").toString())
              .client(httpClient.build()).build();
    }
}

当我运行测试时,我总是在代码android.os.NetworkOnMainThreadException上收到错误.baseUrl(mockWebServer.url("/").toString())。如何正确实现MockWebServer的改装和匕首测试?

1 个答案:

答案 0 :(得分:3)

调用mockWebServer.url("/")启动服务器,这是一个无法在主线程上完成的操作。

我解决了这个问题:

@Provides
@Singleton
MockWebServer providerMockWebServer() {
    MockWebServer mockWebServer = new MockWebServer()
    Thread thread = new Thread() {
        @Override
        public void run() {
            try {
                mockWebServer.start();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    };
    thread.start();
    return mockWebServer;
}