title: Issues when trying to mock Apache HTTP Client with Mockito
body: <p>I am a beginner ramping up on Mockito. I have an HTTP get call which I would like to mock.</p>
  
  <p>The Utility file which has to be tested is this.</p>
  
  <pre><code>import java.io.BufferedReader;
  import java.io.IOException;
  import java.io.InputStreamReader;
  
  import org.apache.http.HttpResponse;
  import org.apache.http.client.ClientProtocolException;
  import org.apache.http.client.HttpClient;
  import org.apache.http.client.methods.HttpGet;
  import org.apache.http.impl.client.DefaultHttpClient;
  
  public class GoogleAccessor {
  private HttpClient client ;
  
      public void getGoogle() throws ClientProtocolException, IOException {
          String url = "http://www.google.com/search?q=httpClient";
           client = new DefaultHttpClient();
          HttpGet request = new HttpGet(url);
          try {
              HttpResponse response = client.execute(request);
              BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
              StringBuffer result = new StringBuffer();
              String line = "";
              while ((line = rd.readLine()) != null) {
                  result.append(line);
              }
              System.out.println(result);
          } catch (Exception e) {
              System.out.println(e);
          }
      }
  
  }
  </code></pre>
  
  <p>This is my jUnit test</p>
  
  <pre><code>import static org.mockito.Mockito.when;
  
  import org.apache.http.ProtocolVersion;
  import org.apache.http.client.HttpClient;
  import org.apache.http.entity.ByteArrayEntity;
  import org.apache.http.message.BasicHttpResponse;
  import org.apache.http.message.BasicStatusLine;
  import org.mockito.Mock;
  import org.junit.Before;
  import org.junit.Test;
  import org.mockito.InjectMocks;
  import org.mockito.Mockito;
  import org.mockito.MockitoAnnotations;
  
  public class TestTest {
  
      @Mock
      private HttpClient mClient;
  
      @InjectMocks
      private GoogleAccessor dummy;
  
      @Before
      public void setUp() throws Exception {
          MockitoAnnotations.initMocks(this);
      }
  
      @Test
      public void testAddCustomer_returnsNewCustomer() throws Exception {
          when(mClient.execute(Mockito.any())).thenReturn(buildResponse(201, "OK", "{\"message\":\"Model was stored\"}"));
          dummy.getGoogle();
      }
  
      BasicHttpResponse buildResponse(int statusCode, String reason, String body) {
          BasicHttpResponse response = new BasicHttpResponse(
                  new BasicStatusLine(new ProtocolVersion("HTTP", 1, 0), statusCode, reason));
          if (body != null) {
              response.setEntity(new ByteArrayEntity(body.getBytes()));
          }
  
          return response;
      }
  }
  </code></pre>
  
  <p>As, mclient isnt initiated, I keep getting this error</p>
  
  <pre><code>java.lang.NullPointerException
      at com.amazon.cartographertests.accessors.TestTest.testAddCustomer_returnsNewCustomer(TestTest.java:34)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      at java.lang.reflect.Method.invoke(Method.java:498)
      ...
  </code></pre>
  
  <p>UPDATE1: I have incorporated comments from @glytching &amp; removed the variable initialization in setup()
  I have incorporated comments from @Maciej Kowalski and made the client a class variable in Utility class</p>
  
  <p>UPDATE 2:</p>
  
  <p>The NPE error has been resolved after resolving to the right import -> import org.mockito.Mock;</p>
  
  <p>But, the issue of not seeing mocked response still exists</p>