简体   繁体   English

如何在 JUnit5 中测试 WireMockServer?

[英]How to test WireMockServer in JUnit5?

I am trying to write a mini-library for testing to mock common external services such as E-mail, SFTP, Buckets, HTTP APIs.我正在尝试编写一个小型库,用于测试模拟常见的外部服务,例如电子邮件、SFTP、存储桶、HTTP API。

At the moment, I got stuck on WireMockServer .目前,我被困在WireMockServer In WireMock docs it states that I can create both server and client to verify API calls.WireMock 文档中,它指出我可以创建服务器和客户端来验证 API 调用。

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.问题是,它失败不是因为断言,而是因为它在被其他进程占用的错误端口 8080 上启动。

How can I start WireMockServer on another port and test it with JUnit 5?如何在另一个端口上启动WireMockServer并使用 JUnit 5 对其进行测试?

I am using Java 8, Maven, Spring Boot.我正在使用 Java 8、Maven、Spring Boot。

As mentioned in comment static verify method tries to verify against default wiremock instance.正如评论中提到的,静态verify方法尝试针对默认的wiremock 实例进行验证。 Since you are creating a standalone instance in your test you should verify against it.由于您在测试中创建了一个独立实例,因此您应该对其进行验证。 Create a verify method in your WireMockTestServer :WireMockTestServer创建一个verify方法:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM