如何在 JUnit5 中测试 WireMockServer?
How to test WireMockServer in JUnit5?
我正在尝试编写一个迷你库来测试模拟常见的外部服务,例如电子邮件、SFTP、Buckets、HTTP APIs。
此刻,我卡在了 WireMockServer
。在 WireMock docs 中,它声明我可以创建服务器和客户端来验证 API 调用。
我写了 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();
}
}
我可以路径端点声明并将我的服务重定向到它。
当我尝试测试它时:
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();
}
}
测试失败。问题是,它失败不是因为断言,而是因为它在错误的端口 8080 上启动,该端口被其他进程占用。
如何在另一个端口上启动 WireMockServer
并使用 JUnit 5 对其进行测试?
我正在使用 Java 8,Maven,Spring Boot.
如注释中所述,静态 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();
// This line should fail
server.verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
server.stop();
}
我正在尝试编写一个迷你库来测试模拟常见的外部服务,例如电子邮件、SFTP、Buckets、HTTP APIs。
此刻,我卡在了 WireMockServer
。在 WireMock docs 中,它声明我可以创建服务器和客户端来验证 API 调用。
我写了 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();
}
}
我可以路径端点声明并将我的服务重定向到它。
当我尝试测试它时:
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();
}
}
测试失败。问题是,它失败不是因为断言,而是因为它在错误的端口 8080 上启动,该端口被其他进程占用。
如何在另一个端口上启动 WireMockServer
并使用 JUnit 5 对其进行测试?
我正在使用 Java 8,Maven,Spring Boot.
如注释中所述,静态 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();
// This line should fail
server.verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
server.stop();
}