Skip to content

Commit 973953f

Browse files
committed
add JUnit5 example and documentation
1 parent 1e460c4 commit 973953f

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,32 @@ public class WireMockContainerExtensionTest {
165165
}
166166
```
167167

168+
```java
169+
@Testcontainers
170+
public class WireMockContainerExtensionJUnit5Test {
171+
172+
@Container
173+
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
174+
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
175+
.withExtension("JSON Body Transformer", Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
176+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "9cookies-wiremock-extensions.jar").toFile()));
177+
178+
@Test
179+
public void testJSONBodyTransformer() throws Exception {
180+
final HttpClient client = HttpClient.newBuilder().build();
181+
final HttpRequest request = HttpRequest.newBuilder()
182+
.uri(wiremockServer.getRequestURI("json-body-transformer"))
183+
.timeout(Duration.ofSeconds(10))
184+
.header("Content-Type", "application/json")
185+
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
186+
187+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
188+
189+
assertThat(response.body()).as("Wrong response body")
190+
.contains("Hello, John Doe!");
191+
}
192+
}
193+
```
168194
## Contributing
169195

170196
All contributions are welcome!
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.wiremock.integrations.testcontainers;
2+
3+
import static org.assertj.core.api.Assertions.assertThat;
4+
5+
import java.net.http.HttpClient;
6+
import java.net.http.HttpRequest;
7+
import java.net.http.HttpResponse;
8+
import java.time.Duration;
9+
import org.junit.jupiter.api.Test;
10+
import org.testcontainers.junit.jupiter.Container;
11+
import org.testcontainers.junit.jupiter.Testcontainers;
12+
13+
@Testcontainers
14+
public class WireMockContainerJUnit5Test {
15+
16+
@Container
17+
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
18+
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
19+
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
20+
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
21+
"hello-world-resource-response.xml");
22+
23+
@Test
24+
public void helloWorld() throws Exception {
25+
final HttpClient client = HttpClient.newBuilder().build();
26+
final HttpRequest request = HttpRequest.newBuilder()
27+
.uri(wiremockServer.getRequestURI("hello"))
28+
.timeout(Duration.ofSeconds(10))
29+
.header("Content-Type", "application/json")
30+
.GET().build();
31+
32+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
33+
34+
assertThat(response.body())
35+
.as("Wrong response body")
36+
.contains("Hello, world!");
37+
}
38+
39+
@Test
40+
public void helloWorldFromFile() throws Exception {
41+
final HttpClient client = HttpClient.newBuilder()
42+
.version(HttpClient.Version.HTTP_1_1)
43+
.build();
44+
45+
HttpRequest request = HttpRequest.newBuilder()
46+
.uri(wiremockServer.getRequestURI("hello-from-file"))
47+
.timeout(Duration.ofSeconds(10))
48+
.header("Content-Type", "application/json")
49+
.GET()
50+
.build();
51+
52+
HttpResponse<String> response =
53+
client.send(request, HttpResponse.BodyHandlers.ofString());
54+
55+
assertThat(response.body())
56+
.as("Wrong response body")
57+
.contains("Hello, world!");
58+
}
59+
}

0 commit comments

Comments
 (0)