0
我使用Java Http Client进行WebService调用。需要使用Mockito进行WebService响应(HttpResponse)的Junit测试。如何为Web服务模拟HttpResponse(使用JavaHttpClient)?
Gson gson = new Gson();
HttpResponse httpResponse= JavaHttpClient.callWebService("URL",object);
String json = EntityUtils.toString(httpResponse.getEntity());
response = gson.fromJson(json, ClassName.class);
log.info("Response: " + new Gson().toJson(response));
如何设置httpResponse.getEntity()的模拟值?这应该被转换成JSON如上
感谢 Nithyanandanķ
'JavaHttpClient'是静态的吗?如果是这样,它不可能在mockito中,但可以在PowerMock中使用。 – Compass
,我已通过创建界面进行了更改。我需要如何模拟response.getentity()值。 –
然后,您可以模拟该接口,创建一个HttpResponse,并填充虚拟数据,然后通过接口模拟返回。 – Compass