Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

246 Commits

Repository files navigation

Testcontainers Java module for WireMock

GitHub release (latest by date)SlackGitHub contributors

NOTE: This project is under development, the GitHub Packages release is coming soon. Not all WireMock features are supported at the moment, and there might be incompatible changes before the 1.0 release. Contributions are welcome!

This module allows provisioning the WireMock server as a standalone container within your unit tests, based on WireMock Docker.

While you can run WireMock Java with the same result for the most of the use-cases, it might be helpful to isolate JVMs or to run on Java versions and platforms not supported by WireMock. A common example is using Wiremock 3.x with Java 1.8.

Compatibility

The module is compatible with the following WireMock versions:

  • WireMock (aka WireMock Java) 2.0.0 and above
  • WireMock (aka WireMock Java) 3.x versions. Note that the official image for WireMock 3 is yet to be released and verified (issue #59)

Other WireMock implementations may work but have not been tested yet. Please feel free to contribute the integration tests and compatibility layers!

Usage

Importing the dependency

The module is published to Maven Central and GitHub Packages. You can also use JitPack to add the dependency in your projects.

Maven

<dependency>
<groupId>org.wiremock.integrations.testcontainers</groupId>
<artifactId>wiremock-testcontainers-module</artifactId>
<version>${see the releases}</version>
<scope>test</scope>
</dependency>

Gradle

dependencies {
testImplementation 'org.wiremock.integrations.testcontainers:wiremock-testcontainers-module:${wiremock-testcontainers.version}'
}

GitHub Packages

GitHub Packages uses the official Maven coordinates (same as Maven Central above), but you will need to configure the server and authentication.

JitPack

JitPack / Maven
 <dependencies>
<dependency>
<groupId>com.github.wiremock</groupId>
<artifactId>wiremock-testcontainers-java</artifactId>
<version>${wiremock-testcontainers.version}</version>
<scope>test</scope>
</dependency>
<!-- .... Other Dependencies -->
</dependencies>
<repositories>
<repository>
<id>jitpack.io</id>
<url>https://jitpack.io</url>
</repository>
</repositories>
JitPack / Gradle
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
dependencies {
testImplementation 'com.github.wiremock:wiremock-testcontainers-java:${wiremock-testcontainers.version}'
}

Using the test container in JUnit 4/5

P.S: Javadoc is coming soon!

Sample Code using JUnit 5

importorg.junit.jupiter.api.*;
importorg.testcontainers.junit.jupiter.*;
importorg.wiremock.integrations.testcontainers.testsupport.http.*;
importstaticorg.assertj.core.api.Assertions.assertThat;
@TestcontainersclassWireMockContainerJunit5Test {
@ContainerWireMockContainerwiremockServer = newWireMockContainer("wiremock/wiremock:2.35.0")
.withMapping("hello", WireMockContainerJunit5Test.class, "hello-world.json");
@TestvoidhelloWorld() throwsException {
// givenStringurl = wiremockServer.getUrl("/hello");
// whenHttpResponseresponse = newTestHttpClient().get(url);
// thenassertThat(response.getBody())
.as("Wrong response body")
.contains("Hello, world!");
}
}

Sample Code using JUnit 4

Show Code
importorg.junit.*;
importorg.wiremock.integrations.testcontainers.testsupport.http.*;
importstaticorg.assertj.core.api.Assertions.assertThat;
publicclassWireMockContainerJunit4Test {
@RulepublicWireMockContainerwiremockServer = newWireMockContainer("wiremock/wiremock:2.35.0")
.withMapping("hello", WireMockContainerJunit4Test.class, "hello-world.json");
@TestpublicvoidhelloWorld() throwsException {
// givenStringurl = wiremockServer.getUrl("/hello");
// whenHttpResponseresponse = newTestHttpClient().get(url);
// thenassertThat(response.getBody())
.as("Wrong response body")
.contains("Hello, world!");
}
}

Using WireMock extensions

The API supports adding WireMock extensions to the test container. The extension can be sourced from the classpath for bundled extensions, or added from the JAR file in the initializer.

Using external extensions

For the external extensions, an extension Jar should be pulled to the test directory before running the test. Apache Maven Dependency Plugin can be used for this purpose. Make sure that all dependencies of the extension JAR, if any, are also included.

Below you can see an examples of using the JSON Body Transformer extension from the 9cookies/wiremock-extensions.

Copying the dependency:

 <plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-dependency-plugin</artifactId>
<version>3.5.0</version>
<executions>
<execution>
<id>copy</id>
<phase>package</phase>
<goals>
<goal>copy</goal>
</goals>
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.ninecookies.wiremock.extensions</groupId>
<artifactId>wiremock-extensions</artifactId>
<version>0.4.1</version>
<classifier>jar-with-dependencies</classifier>
</artifactItem>
</artifactItems>
<outputDirectory>${project.build.directory}/test-wiremock-extension</outputDirectory>
</configuration>
</execution>
</executions>
</plugin>

Mapping definition:

{
"request": {
"method": "POST",
"url": "/json-body-transformer"
},
"response": {
"status": 201,
"headers": {
"content-type": "application/json"
},
"jsonBody": {
"message": "Hello, $(name)!"
},
"transformers" : ["json-body-transformer"]
}
}

Test sample:

Sample code using JUnit 5
importorg.junit.jupiter.api.*;
importorg.testcontainers.junit.jupiter.*;
importorg.wiremock.integrations.testcontainers.testsupport.http.*;
importjava.nio.file.Paths;
importjava.util.Collections;
importstaticorg.assertj.core.api.Assertions.assertThat;
@TestcontainersclassWireMockContainerExtensionJunit5Test {
@ContainerWireMockContainerwiremockServer = newWireMockContainer("wiremock/wiremock:2.35.0")
.withMapping("json-body-transformer", WireMockContainerExtensionJunit5Test.class, "json-body-transformer.json")
.withExtension("JSON Body Transformer",
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
@TestvoidtestJSONBodyTransformer() throwsException {
// givenStringurl = wiremockServer.getUrl("/json-body-transformer");
Stringbody = "{\"name\":\"John Doe\"}";
// whenHttpResponseresponse = newTestHttpClient().post(url, body);
// thenassertThat(response.getBody()).as("Wrong response body")
.contains("Hello, John Doe!");
}
}
Sample code using JUnit 4
Show Code
importorg.junit.*;
importorg.wiremock.integrations.testcontainers.testsupport.http.*;
importjava.nio.file.Paths;
importjava.util.Collections;
importstaticorg.assertj.core.api.Assertions.assertThat;
publicclassWireMockContainerExtensionJunit4Test {
@RulepublicWireMockContainerwiremockServer = newWireMockContainer("wiremock/wiremock:2.35.0")
.withMapping("json-body-transformer", WireMockContainerExtensionJunit4Test.class, "json-body-transformer.json")
.withExtension("JSON Body Transformer",
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
@TestpublicvoidtestJSONBodyTransformer() throwsException {
// givenStringurl = wiremockServer.getUrl("/json-body-transformer");
Stringbody = "{\"name\":\"John Doe\"}";
// whenHttpResponseresponse = newTestHttpClient().post(url, body);
// thenassertThat(response.getBody()).as("Wrong response body")
.contains("Hello, John Doe!");
}
}

Contributing

This repository is implemented as a standard Maven project. All contributions are welcome! Just submit a pull request.

See this page for a generic WireMock Contributor Guide

About

WireMock module for Testcontainers for Java

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages