Skip to content
Advertisement

How to run mock server tests in isolation?

I have these two mock server tests.

When I launch these, the second test fail because the two tests are not launched in isolation. The mocking of the HTTP call in the first method isn’t override in the second method.

@ExtendWith(MockServerExtension.class)
@MockServerSettings(ports = {8888})
class RequestTest {

    private final Client httpClient = ClientBuilder.newClient();

    RequestTest() {
    }

    @Test
    void should_get_fail(MockServerClient client) {
        client.when(request()
                        .withMethod("GET")
                        .withPath("/request_1")
                )
                .respond(response().withStatusCode(500));

        var response = httpClient.target("http://localhost:8888/request_1").request().get();

        assertThat(response.getStatus()).isEqualTo(500);
    }

    @Test
    void should_get_success(MockServerClient client) {
        client.when(request()
                        .withMethod("GET")
                        .withPath("/request_1")
                )
                .respond(response().withStatusCode(200));

        var response = httpClient.target("http://localhost:8888/request_1").request().get();

        assertThat(response.getStatus()).isEqualTo(200);
    }
}

Is there a Mock Server property or a JUnit property to run these two tests in isolation ?

Advertisement

Answer

At the beginning of each test, simply add this line:

client.reset();
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement