0

I have an endpoint that makes a request to a server to check the status. I use WebClient with vertex. I create a client like this.

WebClient client = WebClient.create(vertx);

and the request looks like the following

client.get(check.url).send(ar -> {
    HttpResponse<Buffer> resp = ar.result();
    if (resp != null) {
        check.response = resp.bodyAsString();
    } else {
        check.response = "";
    }
    logger.debug("Received response from app: {} with result:{} on url:{} : {}",
            check.appName, check.result, check.url, check.response);
    check.result.complete(ar.succeeded());
});

The problem i have is when the client tries to send the request. EDIT: I want to mock that client.send() and return my WebClient with the status I want like 200 or 400. Please be advised that I can't access this WebClient as it's declared under the method I want to test.

I have this test right now that I'm trying to fix. TEST

@Test
public void testWongUrlsAndNormalMode() throws Exception{
    when(healthCheckEndpoint.configuration.getMode()).thenReturn(HealthCheckerConfiguration.HealthCheckMode.NORMAL);
    when(healthCheckEndpoint.configuration.getHealthCheckUrls()).thenReturn(urls);

    Response a = healthCheckEndpoint.checkApplications(); //here is when the client.get(url).send() returns nullPointerException

    assertEquals(a.getStatus(), 500);
    assertEquals(a.getEntity(), "normal mode no app urls");
}

but as mentioned i get a null pointer exception when the WebClient tries to send the request. I have also tried to mock the WebClient but I can't mock the send() method as it is a void method

Theodosis
  • 792
  • 2
  • 7
  • 22
  • 1
    The `send`method being a void method does not prevent you from mocking it. See https://stackoverflow.com/q/2276271/5118762 – brass monkey Jan 08 '20 at 15:22
  • pretty sure you gotta mock the checkApplications method return value. right now you just mock the config methods. – Magelan Jan 08 '20 at 15:23
  • @Magelan all this happening to test this method the check application method. – Theodosis Jan 15 '20 at 10:20
  • @LazerBass also the send method can't actually be mocked because its an object inside the method that I can't access thought test. I can't figure a way to access this object and mock the send request to return 200. – Theodosis Jan 15 '20 at 10:21

0 Answers0