MicroProfile HealthCheckResponse JUnit

21 Views Asked by At

We are writing a JUnit for a method which uses Microprofile Health checks. The following code is throwing errors due to incorrect use of mock.

try (MockedStatic<HealthCheckResponse> utilities = Mockito.mockStatic(HealthCheckResponse.class))
    {
        utilities.when(() -> HealthCheckResponse.named(anyString()))
            .thenReturn(HealthCheckResponse.builder().name("connectivity-check").up().build());

        final ConnectivityCheck connectivityCheck = new ConnectivityCheck();
        final HealthCheckResponse healthCheckResponse = connectivityCheck.call();

        assertEquals("connectivity-check", healthCheckResponse.getName());
        assertEquals(HealthCheckResponse.Status.UP, healthCheckResponse.getStatus());
    }

The following exception is thrown.

    org.mockito.exceptions.misusing.UnfinishedStubbingException: 
Unfinished stubbing detected here:
-> at com.utils.health.ConnectivityCheckTest.testConnectivityCheckSuccess(ConnectivityCheckTest.java:75)

E.g. thenReturn() may be missing.
Examples of correct stubbing:
    when(mock.isOk()).thenReturn(true);
    when(mock.isOk()).thenThrow(exception);
    doThrow(exception).when(mock).someVoidMethod();
Hints:
 1. missing thenReturn()
 2. you are trying to stub a final method, which is not supported
 3. you are stubbing the behaviour of another mock inside before 'thenReturn' instruction is completed

It is something to do with the following line. Issue is with mocking HealthCheckResponse class since it has static methods.

utilities.when(() -> HealthCheckResponse.named(anyString()))
            .thenReturn(HealthCheckResponse.builder().name("connectivity-check").up().build());

Please let me know if there is any example of writing a test case for Microprofile Health checks with mocking.

0

There are 0 best solutions below