|
1 | 1 | # TestContainers module for WireMock |
2 | 2 |
|
| 3 | +> NOTE: This project is under development. |
| 4 | +> Not all WireMock features are supported at the moment, |
| 5 | +> and there might be incompatible changes before the 1.0 release. |
| 6 | +> Contributions are welcome! |
| 7 | +
|
| 8 | +This module allows provisioning the WireMock server |
| 9 | +as a standalone container |
| 10 | +within your unit test, based on [WireMock Docker](https://github.com/wiremock/wiremock-docker). |
| 11 | + |
| 12 | +While you can run [WireMock Java](https://github.com/wiremock/wiremock) |
| 13 | +with the same result for the most of the use-cases, |
| 14 | +it might be helpful to isolate JVMs or to run on |
| 15 | +Java versions and platforms not supported by WireMock. |
| 16 | +A common example is using Wiremock 3.x with Java 1.8. |
| 17 | + |
| 18 | +## Usage |
| 19 | + |
| 20 | +Import the dependency: |
| 21 | + |
| 22 | +```xml |
| 23 | + <dependency> |
| 24 | + <groupId>org.wiremock.integrations.testcontainers</groupId> |
| 25 | + <artifactId>wiremock-testcontainers-module</artifactId> |
| 26 | + <version>${see the releases}</version> |
| 27 | + <scope>test</scope> |
| 28 | + </dependency> |
| 29 | +``` |
| 30 | + |
| 31 | +Use it in your Unit tests. |
| 32 | +Javadoc is coming soon! |
| 33 | + |
| 34 | +```java |
| 35 | +import org.wiremock.integrations.testcontainers.WireMockContainer; |
| 36 | +import org.junit.Rule; |
| 37 | +import org.junit.Test; |
| 38 | + |
| 39 | +import java.net.http.HttpClient; |
| 40 | +import java.net.http.HttpRequest; |
| 41 | +import java.net.http.HttpResponse; |
| 42 | +import java.time.Duration; |
| 43 | + |
| 44 | +public class WireMockContainerTest { |
| 45 | + |
| 46 | + @Rule |
| 47 | + public WireMockContainer wiremockServer = new WireMockContainer("2.35.0") |
| 48 | + .withMapping("hello", WireMockContainerTest.class, "hello-world.json"); |
| 49 | + |
| 50 | + @Test |
| 51 | + public void helloWorld() throws Exception { |
| 52 | + final HttpClient client = HttpClient.newBuilder() |
| 53 | + .version(HttpClient.Version.HTTP_1_1).build(); |
| 54 | + |
| 55 | + HttpRequest request = HttpRequest.newBuilder() |
| 56 | + .uri(wiremockServer.getRequestURI("hello")) |
| 57 | + .timeout(Duration.ofSeconds(10)) |
| 58 | + .header("Content-Type", "application/json") |
| 59 | + .GET().build(); |
| 60 | + |
| 61 | + HttpResponse<String> response = |
| 62 | + client.send(request, HttpResponse.BodyHandlers.ofString()); |
| 63 | + |
| 64 | + assertThat(response.body()) |
| 65 | + .as("Wrong response body") |
| 66 | + .contains("Hello, world!"); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
| 70 | + |
| 71 | +## Contributing |
| 72 | + |
| 73 | +All contributions are welcome! |
| 74 | +Just submit a pull request. |
| 75 | + |
0 commit comments