我已经为 okHttp SocketTimeoutException编写了这样的测试用例:
@Test
public void contactDetailsTimeOut() throws Exception {
server.enqueue(new MockResponse().setBody("{}").setBodyDelay(31, TimeUnit.SECONDS));
HttpUrl baseUrl = server.url("/v1/contact/");
userProfilePresenter.contactDetails(baseUrl.toString());
verify(userProfileView).error(isA(SocketTimeoutException.class), isA(String.class));
}
同样可以通过将setSocketPolicy设置为SocketPolicy.NO_RESPONSE来实现
@Test
public void contactDetailsTimeOut() throws Exception {
server.enqueue(new MockResponse().setBody("{}").setSocketPolicy(SocketPolicy.NO_RESPONSE));
HttpUrl baseUrl = server.url("/v1/contact/");
userProfilePresenter.contactDetails(baseUrl.toString());
verify(userProfileView).error(isA(SocketTimeoutException.class), isA(String.class));
}
因为我已将 okHttp readTimeout设置为 30 秒。此测试用例等待 30 秒通过。预期的行为。下面是okHttpClient的代码
private static OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(30, TimeUnit.SECONDS)
.build();
我的问题是:有什么方法可以在更短的时间内测试 SocketTimeoutException?我不想减少客户端的时间。