title: Mocking HttpClient requests with mockito
body: <p>I have the following code that I wish to test using Junit and Mockito.</p>
  
  <p>Code to test:</p>
  
  <pre><code>    Header header = new BasicHeader(HttpHeaders.AUTHORIZATION,AUTH_PREAMBLE + token);
      List&lt;Header&gt; headers = new ArrayList&lt;Header&gt;();
      headers.add(header);
      HttpClient client = HttpClients.custom().setDefaultHeaders(headers).build();
      HttpGet get = new HttpGet("real REST API here"));
      HttpResponse response = client.execute(get);
      String json_string_response = EntityUtils.toString(response.getEntity());
  </code></pre>
  
  <p>And the Test</p>
  
  <pre><code>protected static HttpClient mockHttpClient;
  protected static HttpGet mockHttpGet;
  protected static HttpResponse mockHttpResponse;
  protected static StatusLine mockStatusLine;
  protected static HttpEntity mockHttpEntity;
  
  
  
  
  
  @BeforeClass
  public static void setup() throws ClientProtocolException, IOException {
      mockHttpGet = Mockito.mock(HttpGet.class);
      mockHttpClient = Mockito.mock(HttpClient.class);
      mockHttpResponse = Mockito.mock(HttpResponse.class);
      mockStatusLine = Mockito.mock(StatusLine.class);
      mockHttpEntity = Mockito.mock(HttpEntity.class);
  
      Mockito.when(mockHttpClient.execute(Mockito.isA(HttpGet.class))).thenReturn(mockHttpResponse);
      Mockito.when(mockHttpResponse.getStatusLine()).thenReturn(mockStatusLine);
      Mockito.when(mockStatusLine.getStatusCode()).thenReturn(HttpStatus.SC_OK);
      Mockito.when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity);
  
  }
  
  
  @Test
  underTest = new UnderTest(initialize with fake API (api));
  //Trigger method to test
  </code></pre>
  
  <p>This gives me an error: </p>
  
  <blockquote>
    <p>java.net.UnknownHostException: api: nodename nor servname provided, or
    not known</p>
  </blockquote>
  
  <p>Why does t not mock the <code>'client.execute(get)'</code> call as is in the set up?</p>