如何在JUnit5中测试WireMockServer?

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

How to test WireMockServer in JUnit5?

问题

以下是翻译好的内容:

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

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

我编写了以下类:

  1. public class WireMockTestServer {
  2. private final WireMockServer server;
  3. public WireMockTestServer(String address, MappingBuilder mappingBuilder) {
  4. server = new WireMockServer(wireMockConfig().dynamicPort().dynamicHttpsPort());
  5. }
  6. public WireMockTestServer(int httpPort, int httpsPort, String address, MappingBuilder mappingBuilder) {
  7. server = setup(
  8. new WireMockServer(wireMockConfig().port(httpPort).httpsPort(httpsPort).bindAddress(address)),
  9. mappingBuilder
  10. );
  11. }
  12. private WireMockServer setup(WireMockServer server, MappingBuilder mappingBuilder) {
  13. server.stubFor(mappingBuilder);
  14. return server;
  15. }
  16. public void start() {
  17. server.start();
  18. }
  19. public void stop() {
  20. server.stop();
  21. }
  22. }

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

当我尝试进行测试时:

  1. public class WireMockTestServerTest {
  2. @Test
  3. public void testSetup() throws Exception {
  4. MappingBuilder mappingBuilder = get(urlEqualTo("/health"))
  5. .willReturn(aResponse().withHeader("Content-Type", "application/json")
  6. .withStatus(200));
  7. WireMockTestServer server = new WireMockTestServer(8888, 9988, "127.0.0.1", mappingBuilder);
  8. server.start();
  9. // 这一行应该失败
  10. verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
  11. server.stop();
  12. }
  13. }

测试失败了。问题不在于断言,而是因为它在错误的端口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:

  1. public class WireMockTestServer {
  2. private final WireMockServer server;
  3. public WireMockTestServer(String address, MappingBuilder mappingBuilder) {
  4. server = new WireMockServer(wireMockConfig().dynamicPort().dynamicHttpsPort());
  5. }
  6. public WireMockTestServer(int httpPort, int httpsPort, String address, MappingBuilder mappingBuilder) {
  7. server = setup(
  8. new WireMockServer(wireMockConfig().port(httpPort).httpsPort(httpsPort).bindAddress(address)),
  9. mappingBuilder
  10. );
  11. }
  12. private WireMockServer setup(WireMockServer server, MappingBuilder mappingBuilder) {
  13. server.stubFor(mappingBuilder);
  14. return server;
  15. }
  16. public void start() {
  17. server.start();
  18. }
  19. public void stop() {
  20. server.stop();
  21. }
  22. }

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

When I am trying to test it:

  1. public class WireMockTestServerTest {
  2. @Test
  3. public void testSetup() throws Exception {
  4. MappingBuilder mappingBuilder = get(urlEqualTo("/health"))
  5. .willReturn(aResponse().withHeader("Content-Type", "application/json")
  6. .withStatus(200));
  7. WireMockTestServer server = new WireMockTestServer(8888, 9988, "127.0.0.1", mappingBuilder);
  8. server.start();
  9. // This line should fail
  10. verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
  11. server.stop();
  12. }
  13. }

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 方法:

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

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

  1. @Test
  2. public void testSetup() throws Exception {
  3. MappingBuilder mappingBuilder = get(urlEqualTo("/health"))
  4. .willReturn(aResponse().withHeader("Content-Type", "application/json")
  5. .withStatus(200));
  6. WireMockTestServer server = new WireMockTestServer(8888, 9988, "127.0.0.1", mappingBuilder);
  7. server.start();
  8. // 这一行应该失败
  9. server.verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
  10. server.stop();
  11. }
英文:

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 :

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

and then you can verify against it :

  1. @Test
  2. public void testSetup() throws Exception {
  3. MappingBuilder mappingBuilder = get(urlEqualTo("/health"))
  4. .willReturn(aResponse().withHeader("Content-Type", "application/json")
  5. .withStatus(200));
  6. WireMockTestServer server = new WireMockTestServer(8888, 9988, "127.0.0.1", mappingBuilder);
  7. server.start();
  8. // This line should fail
  9. server.verify(getRequestedFor(urlEqualTo("/health")).withHeader("Content-Type", equalTo("text/xml")));
  10. server.stop();
  11. }

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:

确定