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