title: How to mock HttpClient using Mockito
body: <p>This is my Actual class for which i am writing junit. I have HtpClient as private and final.</p>
  <pre><code> public class KMSHttpClientImpl implements KMSHttpClient
   {
  /**
   * ObjectMapper Instance.
   */
  private final ObjectMapper objectMapper = new ObjectMapper ();
  
  /**
   * KMS ConnectionManager Instance.
   */
  private final KMSHttpConnectionManager kmsHttpConnectionManager =
          new KMSHttpConnectionManagerImpl ();
  
  /**
   * HttpClient object.
   */
  
  private final HttpClient httpClient;
  
  /**
   * KMSHttpClient constructor.
   */
  public KMSHttpClientImpl ()
  {
      // TODO PoolingHttpClientConnectionManager object should be closed after use.
      // TODO This needs to be either singleton or should be kept in static block
      final PoolingHttpClientConnectionManager connectionManager =
              kmsHttpConnectionManager.getConnectionManager();
      httpClient = HttpClients.custom()
              .setConnectionManager(connectionManager)
              .build();
  }
  
  @Override
  public &lt;T&gt; T invokeGETRequest (final String url, final Class&lt;T&gt; clazz)
          throws KMSClientException
  {
      final HttpGet httpGet = new HttpGet(url);
      try {
          final HttpResponse response = httpClient.execute(httpGet);
          return objectMapper.readValue(
                  response.getEntity().getContent(), clazz);
      } catch (IOException e) {
          throw new KMSClientException(&quot;Unable to get the result&quot;, e);
      }
  }
  
  @Override
  public &lt;T&gt; T invokePOSTRequest (final String url, final Object object, final Class&lt;T&gt; clazz)
          throws KMSClientException
  {
      final HttpPost httpPost = new HttpPost(url);
      try {
          final HttpResponse response = httpClient.execute(httpPost);
          return objectMapper.readValue(
                  response.getEntity().getContent(), clazz);
      } catch (IOException e) {
          throw new KMSClientException(&quot;Unable to create the request&quot;, e);
      }
  }
   }
  </code></pre>
  <p>This is my testclass. I am trying to Mock HttpClient but as it is final i cant mock it. And if i remove final from HttpClient in my KMSHttpClientImpl.java class. I am getting PMd issue saying
  Private field 'httpClient' could be made final; it is only initialized in the declaration or constructor. What can i do to fix this issue?</p>
  <pre><code>public class KMSHttpClientImplTest
  {
  
  /**
   * Injecting mocks KMSHttpClientImpl.
   */
  @InjectMocks
  private KMSHttpClientImpl kmsHttpClientImpl;
  
  /**
   * Mock HttpClient.
   */
  @Mock
  private HttpClient httpClient;
  
  
  /**
   * Initial SetUp Method.
   */
  @Before
  public void setUp ()
  {
      initMocks(this);
  }
  
  /**
   * Method to test postRequest Method.
   * @throws KMSClientException
   */
  @Test
  public void testPostRequest () throws KMSClientException
  {
      final OrganizationRequest request = getOrganizationRequest();
      final HttpResponse response = prepareResponse(HttpStatus.SC_OK);
      try {
          Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
          final OrganizationResponse organizationResponse = kmsHttpClientImpl.invokePOSTRequest(
                  ORG_TEST_URL, request, OrganizationResponse.class);
          assertEquals(&quot;Id should match&quot;, ORG_ID, organizationResponse.getId());
      } catch (IOException e) {
          throw new KMSClientException(&quot;Unable to create the request&quot;, e);
      }
        }
  
  /**
   * Method to test getRequest Method.
   * @throws KMSClientException
   */
  @Test
  public void testGetRequest () throws KMSClientException
  {
      try {
          final HttpResponse response = prepareResponse(HttpStatus.SC_OK);
          Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
          final OrganizationResponse organizationResponse = kmsHttpClientImpl.invokeGETRequest
                  (ORG_TEST_URL, OrganizationResponse.class);
          assertEquals(&quot;Id should match&quot;, ORG_ID, organizationResponse.getId());
      }  catch (IOException e) {
          throw new KMSClientException(&quot;Unable to create the request&quot;, e);
      }
  }
  
  /**
   * Method to organizationRequest Object.
   * @return OrganizationRequest object
   */
  public OrganizationRequest getOrganizationRequest ()
  {
      return OrganizationRequest.builder().id(&quot;test&quot;).build();
  }
  
  /**
   * Method to getOrganizationResponse String.
   * @return String Object
   */
  public String getOrganizationResponse ()
  {
      final Map obj=new HashMap();
      obj.put(&quot;id&quot;, ORG_ID);
      obj.put(&quot;uuid&quot;, ORG_UUID);
      obj.put(&quot;orgKeyId&quot;, ORG_KEYID);
      return JSONValue.toJSONString(obj);
  }
  
  /**
   * Method to prepare Response.
   * @param expectedResponseStatus
   * @return HttpResponse
   */
  private HttpResponse prepareResponse (final int expectedResponseStatus)
  {
      final HttpResponse response = new BasicHttpResponse(new BasicStatusLine(
              new ProtocolVersion(&quot;HTTP&quot;, 1, 1),
              expectedResponseStatus, &quot;&quot;));
      response.setStatusCode(expectedResponseStatus);
      final HttpEntity httpEntity = new StringEntity(getOrganizationResponse(),
              ContentType.APPLICATION_JSON);
      response.setEntity(httpEntity);
      return response;
       }
      }
  </code></pre>