Skip to content

sparkmuse/wiremock-junit-jupiter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Wiremock Junit Jupiter

Build Quality Gate Status Coverage Maven Central

Project to use Wiremock with Junit5.

Install

All needed to start using the project is to add the dependency to the POM and that's it.

<dependency>
    <groupId>com.github.sparkmuse</groupId>
    <artifactId>wiremock-junit-jupiter</artifactId>
    <version>${version}</version>
</dependency>

or to gradle

compile 'com.github.sparkmuse:wiremock-junit-jupiter:${version}'

Usage

Simple example

  1. Extend the test class with @ExtendWith(WiremockExtension.class)
  2. Annotate the WireMockServer with @Wiremock
@ExtendWith(WiremockExtension.class)
public class SimpleServerTest {

    @Wiremock
    private WireMockServer server;

    @Test
    @DisplayName("simple test")
    void posts() {

        server.stubFor(get(urlEqualTo("/posts")).willReturn(aResponse().withStatus(200)));

        HttpClient client = HttpClient.newBuilder().build();
        HttpResponse<String> postsResponse = client.send(HttpRequest.newBuilder()
                .uri(URI.create("http://localhost:8080/posts"))
                .GET()
                .build(), HttpResponse.BodyHandlers.ofString());

        assertEquals(200, postsResponse.statusCode());
    }
}

Multiple servers

Multiple servers can be set to improve code readability

@ExtendWith(WiremockExtension.class)
public class MultipleServerTest {

    @Wiremock(port = 9000)
    private WireMockServer postsServer;

    @Wiremock(port = 8000)
    private WireMockServer statistics;

    @Test
    @DisplayName("uses multiple wiremock servers to improve readability")
    void posts() {
        // rest of the test
    }
}

Customize it your way

If the simple options that come with the @Wiremock annotation feel free to add your own configuration options. The instance will be managed automatically.

@ExtendWith(WiremockExtension.class)
public class InstantiatedOptionsServerTest {

    @Wiremock
    private final WireMockServer postsServer = new WireMockServer(
            WireMockConfiguration.options()
                    .port(9000)
                    .containerThreads(20));

    @Test
    @DisplayName("uses values from instance wiremock server")
    void posts() {
        // rest of the test
    }
}

Nested

It supports nested tests.

@ExtendWith(WiremockExtension.class)
class NestedServerTest {

    @Wiremock(port = 9000)
    private WireMockServer parentSever;

    @Test
    @DisplayName("some other top level test")
    void posts() {
        // some other test
    }

    @Nested
    class NestedClass {

        @Wiremock(port = 5000)
        private WireMockServer nestedServer;

        @Test
        @DisplayName("uses parent and nested wiremock server")
        void posts() throws Exception {

            parentSever.stubFor(get(urlEqualTo("/parent")).willReturn(aResponse().withStatus(200)));
            nestedServer.stubFor(get(urlEqualTo("/nested")).willReturn(aResponse().withStatus(200)));

            HttpClient client = HttpClient.newBuilder().build();

            HttpResponse<String> parentResponse = client.send(HttpRequest.newBuilder()
                    .uri(URI.create("http://localhost:9000/parent"))
                    .GET()
                    .build(), HttpResponse.BodyHandlers.ofString());

            HttpResponse<String> nestedResponse = client.send(HttpRequest.newBuilder()
                    .uri(URI.create("http://localhost:5000/nested"))
                    .GET()
                    .build(), HttpResponse.BodyHandlers.ofString());

            assertEquals(200, parentResponse.statusCode());
            assertEquals(200, nestedResponse.statusCode());
        }
    }
}

More examples

The example above and more can be found here: Wiremock junit Jupiter examples