0

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
    You are trying to verify against default instance but you are configuring a standalone instance. You should verify against the instance which is private in your field. Commented Sep 17, 2020 at 15:04

1 Answer 1

1

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.