我正在尝试构造一个CloseableHttpResponse模拟对象,该模拟对象将在我的单元测试之一中返回,但是没有构造函数。我找到了此DefaultHttpResponseFactory,但它仅产生HttpResponse。构造CloseableHttpResponse的简单方法是什么?我需要在测试中呼叫execute()
,然后设置statusLine
和entity
吗?这似乎是一种奇怪的方法。
这是我要模拟的方法:
public static CloseableHttpResponse getViaProxy(String url, String ip, int port, String username,
String password) {
CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(
new AuthScope(ip, port),
new UsernamePasswordCredentials(username, password));
CloseableHttpClient httpclient = HttpClients.custom()
.setDefaultCredentialsProvider(credsProvider).build();
try {
RequestConfig config = RequestConfig.custom()
.setProxy(new HttpHost(ip, port))
.build();
HttpGet httpGet = new HttpGet(url);
httpGet.setConfig(config);
LOGGER.info("executing request: " + httpGet.getRequestLine() + " via proxy ip: " + ip + " port: " + port +
" username: " + username + " password: " + password);
CloseableHttpResponse response = null;
try {
return httpclient.execute(httpGet);
} catch (Exception e) {
throw new RuntimeException("Could not GET with " + url + " via proxy ip: " + ip + " port: " + port +
" username: " + username + " password: " + password, e);
} finally {
try {
response.close();
} catch (Exception e) {
throw new RuntimeException("Could not close response", e);
}
}
} finally {
try {
httpclient.close();
} catch (Exception e) {
throw new RuntimeException("Could not close httpclient", e);
}
}
}
这是使用PowerMockito的模拟代码:
mockStatic(HttpUtils.class);
when(HttpUtils.getViaProxy("http://www.google.com", anyString(), anyInt(), anyString(), anyString()).thenReturn(/*mockedCloseableHttpResponseObject goes here*/)
执行以下步骤可能会帮助:
1.mock it(ex.mockito)
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
HttpEntity entity = mock(HttpEntity.class);
2。应用一些规则
when(response.getStatusLine()).thenReturn(new BasicStatusLine(HttpVersion.HTTP_1_1, HttpStatus.SC_OK, "FINE!"));
when(entity.getContent()).thenReturn(getClass().getClassLoader().getResourceAsStream("result.txt"));
when(response.getEntity()).thenReturn(entity);
3。使用它
when(httpClient.execute((HttpGet) any())).thenReturn(response);
问这个问题已经有一段时间了,但是我想提供我使用的解决方案。
我创建了一个小类,该小类扩展了BasicHttpResponse
类并实现了CloseableHttpResponse
接口(除了关闭响应的方法之外,没有其他方法)。由于BasicHttpResponse
类包含几乎所有内容的setter方法,因此可以使用以下代码段设置所需的所有字段:
public static CloseableHttpResponse buildMockResponse() throws FileNotFoundException {
ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
String reasonPhrase = "OK";
StatusLine statusline = new BasicStatusLine(protocolVersion, HttpStatus.SC_OK, reasonPhrase);
MockCloseableHttpResponse mockResponse = new MockCloseableHttpResponse(statusline);
BasicHttpEntity entity = new BasicHttpEntity();
URL url = Thread.currentThread().getContextClassLoader().getResource("response.txt");
InputStream instream = new FileInputStream(new File(url.getPath()));
entity.setContent(instream);
mockResponse.setEntity(entity);
return mockResponse;
}
我基本上设置了所有由实际代码使用的字段。这还包括将模拟响应内容从文件读取到流中。
我也想创建一个具体的CloseableHttpResponse而不是模拟,所以我在Apache HTTP客户端源代码中对其进行了跟踪。
在MainClientExec中,execute的所有返回值如下:
return new HttpResponseProxy(response, connHolder);
其中connHolder可以为null。
HttpResponseProxy只是一个薄包装纸,它对connHolder进行了封闭。不幸的是,它受到程序包保护,因此(不必要)不可见。
我所做的是创建一个“ PublicHttpResponseProxy”
package org.apache.http.impl.execchain;
import org.apache.http.HttpResponse;
public class PublicHttpResponseProxy extends HttpResponseProxy {
public PublicHttpResponseProxy(HttpResponse original) {
super(original, null);
}
}
必须位于软件包“ org.apache.http.impl.execchain”中!
现在我可以使用实例化具体的CloseableHttpResponse
CloseableHttpResponse response = new PublicHttpResponseProxy(basicResponse);
通常需要注意的地方。由于代理受程序包保护,因此它不是官方API的一部分,因此您可能会掷骰子,以使其稍后无法使用。另一方面,它没有太多内容,因此您可以轻松地编写自己的版本。会有一些粘贴,但还不错。
nvm,我最终只是使用execute()
对其进行了入侵:
很容易创建一个附带现有BasicHttpResponse类型的测试实现: