私はHTTPクライアント用のユニットテストケースを作成しようとしていますが、mockitoを使用してサーバから受け取ったレスポンスを模擬したいと考えています。java httpクライアント用のMockクラスの作成
public HttpResponse postRequest(String uri, String body) throws IOException {
HttpResponse response;
String url = baseUrl + uri;
try (CloseableHttpClient httpClient = HttpClientBuilder.create()
.build()) {
HttpPost post = new HttpPost(url);
post.setEntity(new StringEntity(body));
post.setHeader(AUTHORIZATION_HEADER, authorization);
post.setHeader(CONTENTTYPE_HEADER, APPLICATION_JSON);
post.setHeader(ACCEPT_HEADER, APPLICATION_JSON);
response = httpClient.execute(post);
} catch (IOException e) {
System.out.println("Caught an exception" + e.getMessage().toString());
logger.error("Caught an exception" + e.getMessage().toString());
throw e;
}
return response;
}
私のテストクラスは以下の通りです。私はどのように私の返答体を送るべきかを理解することができません。
public class HTTPRequestTest extends Mockito {
private String body = "{a:b}";
@Test
public void xyz throws Exception {
HttpClient httpClient = mock(HttpClient.class);
HttpPost httpPost = mock(HttpPost.class);
HttpResponse httpResponse = mock(HttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
when(httpClient.execute(httpPost)).thenReturn(body);
}
}