【发布时间】:2020-07-02 11:49:46
【问题描述】:
public HttpResponse mockHttpResponse(int status, Token token) throws Exception {
HttpResponse httpResponse = Mockito.mock(HttpResponse.class);
StatusLine statusLine = mock(StatusLine.class);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(statusLine.getStatusCode()).thenReturn(status);
HttpEntity httpEntity = mock(HttpEntity.class);
when(httpResponse.getEntity()).thenReturn(httpEntity);
when(httpEntity.getContentType()).thenReturn(new BasicHeader(HTTP.CONTENT_TYPE, ContentType.APPLICATION_JSON.toString()),
new BasicHeader(HttpHeaders.CACHE_CONTROL, "100"));
ObjectMapper objectMapper = new ObjectMapper();
byte[] bytes = objectMapper.writeValueAsBytes(token);
when(httpEntity.getContent()).thenReturn(getInputStream(bytes));
return httpResponse;
}
我想检索 response.containsHeader(HttpHeaders.CACHE_CONTROL),但每次在我的测试用例执行中它都返回 false。请建议我如何添加 HttpHeaders.CACHE_CONTROL 作为我的模拟 HttpResponse。
【问题讨论】:
-
java.net.http.HttpResponse 没有方法 containsHeader。您在这里指的是哪种响应类型?
-
org.apache.http.HttpResponse
标签: java unit-testing junit mockito wiremock