单元测试 DefaultHttpRequestRetryHandler
Unit testing DefaultHttpRequestRetryHandler
我正在处理一些将文件存储到远程服务器的遗留代码。我想使用 Apache 的 DefaultHttpRequestRetryHandler 来实现重试逻辑。实施的简化版本如下所示。如何测试我的重试逻辑?
我可以通过重写 DefaultHttpRequestRetryHandler class 中的 retryRequest() 来手动测试它 class 但自动方式会更好。 (我正在使用 Spock 进行测试。)
private CloseableHttpClient getHttpClient() {
DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
CloseableHttpClient httpClient = HttpClients.custom().setRetryHandler(retryHandler).build();
return httpClient;
}
public CloseableHttpResponse uploadFile(){
CloseableHttpClient httpClient = getHttpClient();
CloseableHttpResponse response = null;
try {
response = httpClient.execute(post, getHttpContext());
} catch (Exception ex) {
//handle exception
}
return response;
}
您可能会尝试使用 WireMock,规则如下:
@Rule
public WireMockRule wireMockRule = new WireMockRule(8080);
@Test
public void testRetry()
throws Exception {
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/retry"))
.inScenario("retry")
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("first try").willReturn(aResponse().withBody("error").withStatus(500)));
WireMock.stubFor(
WireMock.get(WireMock.urlEqualTo("/retry"))
.inScenario("retry")
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("first try").willReturn(aResponse().withBody("OK").withStatus(200)));
Integer responseCode = new TestClass().getHttpClient().execute(new HttpHost("localhost", 8080), new HttpGet("http://localhost:8080/retry")).getStatusLine().getStatusCode();
assertThat(responseCode, is(200))
}
我正在处理一些将文件存储到远程服务器的遗留代码。我想使用 Apache 的 DefaultHttpRequestRetryHandler 来实现重试逻辑。实施的简化版本如下所示。如何测试我的重试逻辑?
我可以通过重写 DefaultHttpRequestRetryHandler class 中的 retryRequest() 来手动测试它 class 但自动方式会更好。 (我正在使用 Spock 进行测试。)
private CloseableHttpClient getHttpClient() {
DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler();
CloseableHttpClient httpClient = HttpClients.custom().setRetryHandler(retryHandler).build();
return httpClient;
}
public CloseableHttpResponse uploadFile(){
CloseableHttpClient httpClient = getHttpClient();
CloseableHttpResponse response = null;
try {
response = httpClient.execute(post, getHttpContext());
} catch (Exception ex) {
//handle exception
}
return response;
}
您可能会尝试使用 WireMock,规则如下:
@Rule
public WireMockRule wireMockRule = new WireMockRule(8080);
@Test
public void testRetry()
throws Exception {
WireMock.stubFor(WireMock.get(WireMock.urlEqualTo("/retry"))
.inScenario("retry")
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("first try").willReturn(aResponse().withBody("error").withStatus(500)));
WireMock.stubFor(
WireMock.get(WireMock.urlEqualTo("/retry"))
.inScenario("retry")
.whenScenarioStateIs(Scenario.STARTED)
.willSetStateTo("first try").willReturn(aResponse().withBody("OK").withStatus(200)));
Integer responseCode = new TestClass().getHttpClient().execute(new HttpHost("localhost", 8080), new HttpGet("http://localhost:8080/retry")).getStatusLine().getStatusCode();
assertThat(responseCode, is(200))
}