com.github.sparkmuse:wiremock-junit-jupiter

Project to use Wiremock with Junit5.

License

License

Categories

Categories

JUnit Unit Testing Wire Data Data Structures
GroupId

GroupId

com.github.sparkmuse
ArtifactId

ArtifactId

wiremock-junit-jupiter
Last Version

Last Version

1.3.6
Release Date

Release Date

Type

Type

jar
Description

Description

com.github.sparkmuse:wiremock-junit-jupiter
Project to use Wiremock with Junit5.
Project URL

Project URL

https://github.com/sparkmuse/wiremock-junit-jupiter
Source Code Management

Source Code Management

https://github.com/sparkmuse/wiremock-junit-jupiter

Download wiremock-junit-jupiter

How to add to project

<!-- https://jarcasting.com/artifacts/com.github.sparkmuse/wiremock-junit-jupiter/ -->
<dependency>
    <groupId>com.github.sparkmuse</groupId>
    <artifactId>wiremock-junit-jupiter</artifactId>
    <version>1.3.6</version>
</dependency>
// https://jarcasting.com/artifacts/com.github.sparkmuse/wiremock-junit-jupiter/
implementation 'com.github.sparkmuse:wiremock-junit-jupiter:1.3.6'
// https://jarcasting.com/artifacts/com.github.sparkmuse/wiremock-junit-jupiter/
implementation ("com.github.sparkmuse:wiremock-junit-jupiter:1.3.6")
'com.github.sparkmuse:wiremock-junit-jupiter:jar:1.3.6'
<dependency org="com.github.sparkmuse" name="wiremock-junit-jupiter" rev="1.3.6">
  <artifact name="wiremock-junit-jupiter" type="jar" />
</dependency>
@Grapes(
@Grab(group='com.github.sparkmuse', module='wiremock-junit-jupiter', version='1.3.6')
)
libraryDependencies += "com.github.sparkmuse" % "wiremock-junit-jupiter" % "1.3.6"
[com.github.sparkmuse/wiremock-junit-jupiter "1.3.6"]

Dependencies

compile (3)

Group / Artifact Type Version
com.github.tomakehurst : wiremock-standalone jar 2.27.2
org.junit.jupiter : junit-jupiter-api jar 5.7.1
org.slf4j : slf4j-simple jar 1.7.30

test (1)

Group / Artifact Type Version
org.junit.jupiter : junit-jupiter jar 5.7.1

Project Modules

There are no modules declared in this project.

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

Versions

Version
1.3.6
1.3.5
1.3.4
1.3.2
1.1.18
1.1.15
1.1.13
1.1.12