Not able to Mock Object using Mockito

Viewed 385

I am mocking Httpclient in my KMSHttpClientImplTest class. But it is not getting mocked. I am getting the actual Httpclient instance. Is there anything m doing wrong?

This is my testClass

public class KMSHttpClientImplTest {


@InjectMocks
private KMSHttpClient kmsHttpClient;

HttpClient httpClient;

HttpGet httpGet;

/**
 * Initial SetUp Method
 */
@Before
public void setUp() throws KMSClientException {
    kmsHttpClient = new KMSHttpClientImpl();
    httpClient = Mockito.mock(HttpClient.class);
    httpGet = Mockito.mock(HttpGet.class);
}

@Test
public void testPostRequest () throws IOException {
    OrganizationRequest request = getOrganizationRequest();
    HttpResponse response = prepareResponse(200);
    Mockito.when(httpClient.execute(Mockito.any())).thenReturn(response);
    HttpResponse httpResponse = kmsHttpClient.postRequest("https://www.google.co.in/" , new StringEntity(request.toString()));
    Assert.assertEquals( 200, httpResponse.getStatusLine().getStatusCode());
}

@Test
public void testGetRequest () throws IOException {
    HttpResponse response = prepareResponse(200);
    Mockito.when(httpClient.execute(httpGet)).thenReturn(response);
    HttpResponse httpResponse = kmsHttpClient.getRequest("https://www.test.co.in/");
    Assert.assertEquals(200, httpResponse.getStatusLine().getStatusCode());
}

public OrganizationRequest getOrganizationRequest () {
    return OrganizationRequest.builder().id("test").build();
}

private HttpResponse prepareResponse(int expectedResponseStatus) {
    HttpResponse response = new BasicHttpResponse(new BasicStatusLine(
            new ProtocolVersion("HTTP", 1, 1), expectedResponseStatus, ""));
    response.setStatusCode(expectedResponseStatus);
    return response;
}

}

This is my actual implementation class

public class KMSHttpClientImpl implements KMSHttpClient
{

/**
 * KMS ConnectionManager Instance.
 */
private final KMSHttpConnectionManager kmsHttpConnectionManager =
        new KMSHttpConnectionManager ();

/**
 * 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)
            .setRetryHandler(kmsHttpConnectionManager.retryHandler(RETRY_COUNT)).build();
}

/**
 * Method to execute Post request.
 * @param url
 * @param stringEntity
 * @return HttpResponse
 * @throws IOException
 */
public HttpResponse postRequest (final String url,
                                 final StringEntity stringEntity) throws IOException
{
    final HttpPost httpPost = new HttpPost(url);
    httpPost.setEntity(stringEntity);
    return httpClient.execute(httpPost);
}

/**
 * Method to execute Get request.
 * @param url
 * @return HttpResponse
 * @throws IOException
 */
public HttpResponse getRequest (final String url) throws IOException
{
    final HttpGet httpGet = new HttpGet(url);
    return httpClient.execute(httpGet);
}

}

I am mocking Httpclient in my KMSHttpClientImplTest class. But it is not getting mocked. I am getting the actual Httpclient instance. Is there anything m doing wrong?

1 Answers

In your example you declare new instance that kmsHttpClient = new KMSHttpClientImpl(); so you never get mock for kmsHttpClient. Try to do it:

@InjectMocks
private KMSHttpClient kmsHttpClient;

@Mock
private HttpClient httpClient;

@Mock
private HttpGet httpGet;

/**
 * Use @Before for junit4 or @BeforeEach for junit5
 */
@Before
public void setUp() throws KMSClientException {
    initMocks(this);
}
Related