0

I want to mock execute method of HttpClass to return a dummy response, but my test code is acutally executing the request.

Class Code -

class MyService {

    private CloseableHttpClient newHttpClient() {

        try {
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
                    new SSLContextBuilder().loadTrustMaterial(TrustAllStrategy.INSTANCE).build(),
                    NoopHostnameVerifier.INSTANCE);

            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (KeyManagementException | NoSuchAlgorithmException | KeyStoreException exception) {
            logger.error(exception);
        }

        return null;
    }

    public String create(CreateRequestDTO Request) {

        CloseableHttpClient client = newHttpClient();
        CloseableHttpResponse response = null;

        HttpPut httpPut = new HttpPut("MyUrl...");

        try {
            response = client.execute(httpPut);
        } catch (IOException exception) {
            return "exception";
        }
        return response.toString();
    }
}

Test class code -

@PrepareForTest({ MyService.class })
@PowerMockIgnore({ "com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*", "com.sun.org.apache.xalan.*",
        "javax.activation.*", "javax.management.*", "javax.net.ssl.*", "javax.crypto.*" })
@RunWith(PowerMockRunner.class)
public class MyServiceTest {

    @InjectMocks
    private MyService MyService;

    @Mock
    private CloseableHttpClient closeableHttpClient;

    @Mock
    private HttpClient httpClient;

    @Mock
    private CloseableHttpResponse closeableHttpResponse;

    @Mock
    private HttpPut httpPut;

    @Mock
    private HttpEntity httpEntity;

    @Before
    public void setupTest() {
        MockitoAnnotations.initMocks(this);
    }

    @Test
    public void testCreate() throws Exception {
        // MyService MyServiceSpy = PowerMockito.spy(MyService);
        MyService MyServiceSpy = PowerMockito.spy(MyService);
        CreateRequestDTO createRequestDTO = new CreateRequestDTO();
        createRequestDTO.setLocation("room 1");
        createRequestDTO.setStartTime("3 pm");
        createRequestDTO.setEndTime("4 pm");

        try {

            PowerMockito.doReturn(closeableHttpClient).when(MyServiceSpy, "newHttpClient");

            // PowerMockito.when(MyServiceSpy,
            // "newHttpClient").thenReturn(httpClient);

            // PowerMockito.verifyPrivate(MyServiceSpy).invoke("newHttpClient");

            String jsonEntity = " created successfully";

            HttpEntity httpEntity = EntityBuilder.create().setText(jsonEntity)
                    .setContentType(org.apache.http.entity.ContentType.APPLICATION_JSON).build();

            closeableHttpResponse.setEntity(httpEntity);

            PowerMockito.doReturn(closeableHttpResponse).when(closeableHttpClient).execute(any(HttpPut.class));
            // PowerMockito.doReturn(closeableHttpResponse).when(httpClient).execute(any(HttpPut.class));

            String res = MyService.create(createRequestDTO);
            logger.info("Result from create : " + res);
            Assert.assertTrue(res.substring(0, 5).matches("BEGIN"));

        } catch (Exception e) {
            e.printStackTrace();
            throw e;
        }
    }

}

I have tried Mockito instead instead of PowerMockito, but still request is executed in real.

Nkosi
  • 235,767
  • 35
  • 427
  • 472
Vikash Yadav
  • 713
  • 1
  • 9
  • 29
  • 2
    Possible duplicate of [Mocked HttpClient calls actual method](https://stackoverflow.com/questions/42102696/mocked-httpclient-calls-actual-method). As mentioned (in original thread) the service code need to be refactored so that the mocking can be utilized. In its current form the behavior is as expected. – Bond - Java Bond Jul 26 '19 at 11:45
  • Is this required MyService MyServiceSpy = PowerMockito.spy(MyService); – MangduYogii Jul 26 '19 at 11:49

1 Answers1

1

Your problem is not with mocking the client, but with building it. Currently your code builds a new client instead of the mock, when you use HttpClients.custom() static method.

Since you need to mock a static method, you'll need PowerMockito.mockStatic()

First you need to add HttpClients to your @PrepareForTest annotation, and later you should do:

PowerMockito.mockStatic(HttpClients.class);
when(HttpClients.custom()).thenReturn(mockedBuilder);
when(mockedBuilder.setSSLSocketFactory(any(SSLSocketFactory.class)).theReturn(mockedBuilder);
when(mockedBuilder.build()).thenReturn(httpClient);// your mock object
Nir Levy
  • 12,750
  • 3
  • 21
  • 38
  • 1. I am returning mocked http client using mocking of private method, is it not correct? 2. In solution suggested by you what is mockedBuilder? – Vikash Yadav Jul 29 '19 at 04:22
  • I tried mocking Http client builder using `@Mock private HttpClientBuilder mockedBuilder;` but getting an error `org.mockito.exceptions.misusing.InvalidUseOfMatchersException: Misplaced or misused argument matcher detected here:` on line `when(mockedBuilder.setSSLSocketFactory(any(SSLSocketFactory.class)).thenReturn(mockedBuilder);` – Vikash Yadav Jul 29 '19 at 04:57
  • What is mockedBuilder ? – agaonsindhe Mar 03 '23 at 08:59