title: Using Mockito for HTTP Client
body: <p>I have a stubbed JSON OBJECT but need to mock the following using Mockito:                                           </p>
  
  <pre><code>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()); 
  </code></pre>
  
  <p>I have created the following Mocks: </p>
  
  <pre><code>@Mock
      private HttpClient mockHttpClient;
      private HttpPost mockHttpPost;
      private HttpResponse mockHttpResponse;
      private HttpEntity mockHttpEntity; 
      private InputStream mockInputStream;
      private InputStreamReader mockInputStreamReader;
      private BufferedReader mockBufferedReader;
  </code></pre>
  
  <p>And have the following <code>when</code> statements:</p>
  
  <pre><code>    Mockito.when(mockHttpClient.execute(mockHttpPost)).thenReturn(mockHttpResponse); 
      Mockito.when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity);
      Mockito.when(mockHttpEntity.getContent()).thenReturn(mockInputStream);
  </code></pre>
  
  <p>Question: Do I need to create all these 'when' statements and if yes then which other ones do I need to create to be able to get to the stubbed JSON?</p>
  
  <p>Any suggestions pls?</p>
  
  <p>Thanks</p>