ddovgal / wiremock-junit-jupiter

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Wiremock Junit Jupiter

Build Status 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());

        assertThat(postsResponse.statusCode()).isEqualTo(200);
    }
}

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

            assertThat(parentResponse.statusCode()).isEqualTo(200);
            assertThat(nestedResponse.statusCode()).isEqualTo(200);
        }
    }
}

More examples

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

About


Languages

Language:Java 98.9%Language:Shell 1.1%