如何在JUnit5中测试WireMockServer?

huangapple go评论62阅读模式
英文:

How to test WireMockServer in JUnit5?

问题

以下是翻译好的内容:

我正在尝试编写一个用于测试的迷你库,以模拟常见的外部服务,如电子邮件、SFTP、存储桶和HTTP API。

目前,我在WireMockServer部分遇到了问题。在WireMock文档中指出,我可以创建服务器和客户端来验证API调用。

我编写了以下类:

public class WireMockTestServer {

    private final WireMockServer server;

    public WireMockTestServer(String address, MappingBuilder mappingBuilder) {
        server = new WireMockServer(wireMockConfig().dynamicPort().dynamicHttpsPort());
    }

    public WireMockTestServer(int httpPort, int httpsPort, String address, MappingBuilder mappingBuilder) {
        server = setup(
                new WireMockServer(wireMockConfig().port(httpPort).httpsPort(httpsPort).bindAddress(address)),
                mappingBuilder
        );
    }

    private WireMockServer setup(WireMockServer server, MappingBuilder mappingBuilder) {
        server.stubFor(mappingBuilder);
        return server;
    }

    public void start() {
        server.start();
    }

    public void stop() {
        server.stop();
    }
}

我可以在其中声明端点并将我的服务重定向到它。

当我尝试进行测试时:

public class WireMockTestServerTest {

    @Test
    public void testSetup() throws Exception {
        MappingBuilder mappingBuilder = get(urlEqualTo("/health"))
                .willReturn(aResponse().withHeader("Content-Type", "application/json")
                        .withStatus(200));
        WireMockTestServer server = new WireMockTestServer(8888, 9988, "127.0.0.1", mappingBuilder);
        server.start();

        // 这一行应该失败
        verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
        server.stop();
    }

}

测试失败了。问题不在于断言,而是因为它在错误的端口8080上启动,该端口被其他进程占用。

如何在另一个端口上启动WireMockServer并使用JUnit 5进行测试?

我正在使用Java 8、Maven和Spring Boot。

英文:

I am trying to write a mini-library for testing to mock common external services such as E-mail, SFTP, Buckets, HTTP APIs.

At the moment, I got stuck on WireMockServer. In WireMock docs it states that I can create both server and client to verify API calls.

I wrote the class:

public class WireMockTestServer {

    private final WireMockServer server;

    public WireMockTestServer(String address, MappingBuilder mappingBuilder) {
        server = new WireMockServer(wireMockConfig().dynamicPort().dynamicHttpsPort());
    }

    public WireMockTestServer(int httpPort, int httpsPort, String address, MappingBuilder mappingBuilder) {
        server = setup(
                new WireMockServer(wireMockConfig().port(httpPort).httpsPort(httpsPort).bindAddress(address)),
                mappingBuilder
        );
    }

    private WireMockServer setup(WireMockServer server, MappingBuilder mappingBuilder) {
        server.stubFor(mappingBuilder);
        return server;
    }

    public void start() {
        server.start();
    }

    public void stop() {
        server.stop();
    }
}

which I can path endpoint declaration and redirect my services toward it.

When I am trying to test it:

public class WireMockTestServerTest {

    @Test
    public void testSetup() throws Exception {
        MappingBuilder mappingBuilder = get(urlEqualTo("/health"))
                .willReturn(aResponse().withHeader("Content-Type", "application/json")
                        .withStatus(200));
        WireMockTestServer server = new WireMockTestServer(8888, 9988, "127.0.0.1", mappingBuilder);
        server.start();


        // This line should fail
        verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
        server.stop();
    }

}

The test fails. The issue is, it fails not because of an assertion but because it starts on a wrong port 8080 which is occupied by other processes.

How can I start WireMockServer on another port and test it with JUnit 5?

I am using Java 8, Maven, Spring Boot.

答案1

得分: 1

正如在注释中提到的,静态的 verify 方法尝试针对默认的 WireMock 实例进行验证。由于您在测试中创建了一个独立的实例,您应该针对它进行验证。在您的 WireMockTestServer 中创建一个 verify 方法:

public void verify(final RequestPatternBuilder requestPatternBuilder) {
    server.verify(requestPatternBuilder);
}

然后您可以对其进行验证:

@Test
public void testSetup() throws Exception {
    MappingBuilder mappingBuilder = get(urlEqualTo("/health"))
            .willReturn(aResponse().withHeader("Content-Type", "application/json")
                    .withStatus(200));
    WireMockTestServer server = new WireMockTestServer(8888, 9988, "127.0.0.1", mappingBuilder);
    server.start();

    // 这一行应该失败
    server.verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
    server.stop();
}
英文:

As mentioned in comment static verify method tries to verify against default wiremock instance. Since you are creating a standalone instance in your test you should verify against it. Create a verify method in your WireMockTestServer :

public void verify(final RequestPatternBuilder requestPatternBuilder) {
    server.verify(requestPatternBuilder);
}

and then you can verify against it :

@Test
public void testSetup() throws Exception {
    MappingBuilder mappingBuilder = get(urlEqualTo("/health"))
            .willReturn(aResponse().withHeader("Content-Type", "application/json")
                    .withStatus(200));
    WireMockTestServer server = new WireMockTestServer(8888, 9988, "127.0.0.1", mappingBuilder);
    server.start();

    // This line should fail
    server.verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
    server.stop();
}

huangapple
  • 本文由 发表于 2020年9月17日 22:45:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/63940581.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定