title: Mocked CloseableHttpClient still passes connection to real server while testing
body: <p>i try to test a method which uses a CloseableHttpClient Connection. I decided to go for Mockito and mocked all relevant classes and methods. But still, as soon as I start the test it goes straight to the real server instead of being intercepted by the mocked CloseableHttpClient.</p>
  
  <p>The test</p>
  
  <pre><code>  @Test
      public void testBid() throws IOException {
          //given:
          HttpGet httpGet = mock(HttpGet.class);
          HttpResponse httpResponse = mock(HttpResponse.class);
  
          StatusLine statusLine = mock(StatusLine.class);
  
          ObserverImp observer = mock(ObserverImp.class);
  
          CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
          CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);
  
          //and:
          when(statusLine.getStatusCode()).thenReturn(200);
          when(httpResponse.getStatusLine()).thenReturn(statusLine);
          when(closeableHttpClient.execute(httpGet)).thenReturn(closeableHttpResponse);
          when(observer.getKey()).thenReturn("##213");
  
  
          Buy buy = new Buy(observer);
          buy.bid(14455);
      }
  </code></pre>
  
  <p>And the related <strong>Implementation</strong></p>
  
  <pre><code>public void buy(double price) {
          String queryArgs = "command=order&amp;amount=1" + "&amp;price=" + String.valueOf(price); // generates query
  
          CloseableHttpClient httpClient = HttpClients.createDefault();
          HttpPost post = new HttpPost("wwww.hm.edu/projectGroup1");
          post.addHeader("Key", observer.getKey());
          try {
              post.setEntity(new ByteArrayEntity(queryArgs.getBytes("UTF-8")));
          } catch (UnsupportedEncodingException e) {
              System.out.println("Exception in run");
          }
          List&lt;NameValuePair&gt; params = new ArrayList&lt;&gt;();
  
          params.add(new BasicNameValuePair("command", "order"));
          params.add(new BasicNameValuePair("amount", "1"));
          params.add(new BasicNameValuePair("price", String.valueOf(price)));
          try {
              post.setEntity(new UrlEncodedFormEntity(params));
              CloseableHttpResponse response = httpClient.execute(post);
              HttpEntity entity = response.getEntity();
              Scanner in = new Scanner(entity.getContent());
              String orderNumber = "";
              while (in.hasNext()) {
                  orderNumber = in.nextLine();
              }
              String[] findOrderNumber = orderNumber.split(".");
              long lastOrderNumber = -1;
              try {
                  lastOrderNumber = Long.valueOf(findOrderNumber[3]);
              } catch (NumberFormatException exception) {
                  System.out.println("NumberFormatException");
              } finally {
                  if (lastOrderNumber != -1) {
                      observer.setOrderNumber(lastOrderNumber);
                  }
              }
              in.close();
              EntityUtils.consume(entity);
              httpClient.close();
          } catch (IOException e) {
              System.out.println("Exception occured during process");
          }
      }
  </code></pre>
  
  <p>I tried several code snippets of tests I found in tutorials or similar questions here but with no success. Can you please tell me what I do wrong here?</p>
  
  <p>Thank you :) </p>