我有存根的JSON對象,但需要模擬以下使用的Mockito:使用的Mockito用於HTTP客戶端
HttpResponse response = defaultHttpClient.execute(postRequest);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuilder result = new StringBuilder();
while ((line = rd.readLine()) != null) {
result.append(line);
}
JSONObject jsonResponseObject = new JSONObject(result.toString());
我創建了以下嘲笑:
@Mock
private HttpClient mockHttpClient;
private HttpPost mockHttpPost;
private HttpResponse mockHttpResponse;
private HttpEntity mockHttpEntity;
private InputStream mockInputStream;
private InputStreamReader mockInputStreamReader;
private BufferedReader mockBufferedReader;
,並具有以下when
聲明:
Mockito.when(mockHttpClient.execute(mockHttpPost)).thenReturn(mockHttpResponse);
Mockito.when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity);
Mockito.when(mockHttpEntity.getContent()).thenReturn(mockInputStream);
問題:我是否需要創建所有這些'when'statem如果是的話,那麼我需要創建哪些其他內容以便能夠訪問被截斷的JSON?
任何建議請問?
感謝
請添加一些代碼,以便我們可以瞭解確切的模擬對象 –
您是否在使用Spring MVC之類的任何Web框架? – SergeyB