我想寫一個使用HttpClient
版本4.5
的類的測試。我寫的測試非常慢,所以我試圖找出問題。我可以寫證明我的問題一個試驗:HttpClient和LocalServerTestBase真的很慢
public class RequeteurRESTTest extends LocalServerTestBase {
private String startServer(String urlSuffix, HttpRequestHandler handler) throws Exception{
this.serverBootstrap.registerHandler(urlSuffix, handler);
HttpHost target = start();
String serverUrl = "http://localhost:" + target.getPort();
return serverUrl;
}
@Test
public void testCase() throws Exception{
String baseURL = startServer("/api", new HttpRequestHandler() {
@Override
public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException {
response.setStatusCode(HttpStatus.SC_OK);
}
});
HttpClient httpClient;
httpClient = HttpClients.custom().build();
HttpGet method = new HttpGet(baseURL + "/api");
HttpResponse response = httpClient.execute(method);
}
}
我的問題是,該指令:
httpClient.execute(method)
需要10秒執行。這很慢,但我不明白爲什麼這麼慢。我在測試中犯過什麼錯誤,或者忘記了什麼?
是否正確執行?即被測試的服務器是否被調用? –
處理程序被正確調用,是 –