我试图为我的android项目实现检测测试。在我的主要项目中,我使用了 Dagger 和翻新版,并从我的模块中提供了翻新版。

这是我提供改造的模块:

@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())。如何正确实现带有调试和 Dagger 的MockWebServer进行测试?

最佳答案

调用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;
}

10-08 17:50