0

I have written a code to perform http post operation:

public Response postResponse(
        @NonNull final String payload,
        @NonNull final String endpoint,
        @NonNull final ContentType contentType,
        @NonNull final String clientCertificate
) {
    SSLConnectionSocketFactory sslConnectionSocketFactory;
    if (StringUtils.isNotEmpty(clientCertificate)) {
        // Client certificate is present, thus using TLS
        sslConnectionSocketFactory =
                new SSLConnectionSocketFactory(postUtility
                        .getSocketFactory(clientCertificate),
                        NoopHostnameVerifier.INSTANCE);
    } else {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, null, new java.security.SecureRandom());

        sslConnectionSocketFactory = new SSLConnectionSocketFactory(
                sslcontext,
                new String[]{"TLSv1"}, //{ "TLS10" },
                null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    }

    HttpPost httpPost = new HttpPost(endpoint);
    httpPost.setHeader("Content-Type", contentType.getMimeType());

    httpPost.setEntity(
            EntityBuilder.create()
                    .setText(payload)
                    .setContentType(contentType)
                    .build()
    );

    try (
            CloseableHttpClient httpclient = HttpClients.custom()
                    .setSSLContext(SSLContext.getDefault())
                    .setSSLSocketFactory(sslConnectionSocketFactory)
                    .build();
            CloseableHttpResponse response = httpclient.execute(httpPost)
    ) {
        return Response.status(response.getStatusLine().getStatusCode()).build();
    } catch (Exception ex) {
        log.error("Unable to send response", ex);
        return Response.serverError().build();
    }
}

And wrote below unit test with MockitoExtension.class but it seems to be throwing error: SSLConnectionSocketFactory$MockitoMock$1231118446 cannot be returned by getSocketFactory() getSocketFactory() should return SSLSocketFactory. Return type of getSocketFactory() method in postUtility class returns a SSLSocketFactory. I want to use MockitoExtension only and not PowerMockito

@Mock
private PostUtility postUtility;

@Mock
private SSLConnectionSocketFactory sslConnectionSocketFactory;

@Mock
private SSLSocketFactory sslSocketFactory;

@Mock
private CloseableHttpClient httpclient;

@Mock
private CloseableHttpResponse httpResponse;

@Mock
private StatusLine statusLine;

@Test
public void testPostResponse() throws Exception {
    when(postUtility.getSocketFactory(clientCertificate))
            .thenReturn(sslSocketFactory);
    when(new SSLConnectionSocketFactory(postUtility.getSocketFactory(clientCertificate), NoopHostnameVerifier.INSTANCE))
            .thenReturn(sslConnectionSocketFactory);

    when(httpResponse.getStatusLine()).thenReturn(statusLine);
    when(statusLine.getStatusCode()).thenReturn(200);
    when(httpclient.execute(any(HttpPost.class))).thenReturn(httpResponse);


    Response response = className.postResponse("testPayLoad","testEndPoint",ContentType.TEXT_XML, "testclientCertificate");

    assertEquals(200, response.getStatus(), "Response status code should be 200");
}

1 Answers1

0

You cannot do when(new XYZ()); Mockito cannot create stubbings for constructors.

The statement

when(new SSLConnectionSocketFactory(postUtility.getSocketFactory(clientCertificate), NoopHostnameVerifier.INSTANCE))
        .thenReturn(sslConnectionSocketFactory);

is seen by Mockito as

when(postUtility.getSocketFactory(clientCertificate), NoopHostnameVerifier.INSTANCE)
        .thenReturn(sslConnectionSocketFactory);

and that's the reason for your error: getSocketFactory does not return a SslConnectionSocketFactory.

Possible solutions:

knittl
  • 246,190
  • 53
  • 318
  • 364