如何 运行 隔离模拟服务器测试?

How to run mock server tests in isolation?

我有这两个模拟服务器测试。

当我启动这些时,第二个测试失败了,因为这两个测试不是单独启动的。第一种方法中对 HTTP 调用的模拟不会在第二种方法中被覆盖。

@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);
    }
}

是否有Mock Server 属性 或JUnit 属性 来运行 隔离这两个测试?

在每个测试的开头,只需添加这一行:

client.reset();