Skip to content

Commit ee392b6

Browse files
Merge pull request #16 from kapishmalik/feature/add-JUnit5-example-and-documentation
Add JUnit5 test coverage and documentation
2 parents 76326e3 + 4d7f69e commit ee392b6

File tree

2 files changed

+133
-0
lines changed

2 files changed

+133
-0
lines changed

README.md

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,49 @@ A common example is using Wiremock 3.x with Java 1.8.
3434

3535
P.S: Javadoc is coming soon!
3636

37+
#### Sample Code using JUnit 5
38+
39+
```java
40+
import static org.assertj.core.api.Assertions.assertThat;
41+
42+
import java.net.http.HttpClient;
43+
import java.net.http.HttpRequest;
44+
import java.net.http.HttpResponse;
45+
import java.time.Duration;
46+
import org.junit.jupiter.api.Test;
47+
import org.testcontainers.junit.jupiter.Container;
48+
import org.testcontainers.junit.jupiter.Testcontainers;
49+
50+
@Testcontainers
51+
public class WireMockContainerJUnit5Test {
52+
53+
@Container
54+
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
55+
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
56+
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
57+
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
58+
"hello-world-resource-response.xml");
59+
60+
@Test
61+
public void helloWorld() throws Exception {
62+
final HttpClient client = HttpClient.newBuilder().build();
63+
final HttpRequest request = HttpRequest.newBuilder()
64+
.uri(wiremockServer.getRequestURI("hello"))
65+
.timeout(Duration.ofSeconds(10))
66+
.header("Content-Type", "application/json")
67+
.GET().build();
68+
69+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
70+
71+
assertThat(response.body())
72+
.as("Wrong response body")
73+
.contains("Hello, world!");
74+
}
75+
}
76+
```
77+
78+
#### Sample Code using JUnit 4
79+
3780
```java
3881
import org.wiremock.integrations.testcontainers.WireMockContainer;
3982
import org.junit.Rule;
@@ -140,6 +183,37 @@ Mapping definition:
140183

141184
Test sample:
142185

186+
##### Sample code using JUnit 5
187+
188+
```java
189+
@Testcontainers
190+
public class WireMockContainerExtensionJUnit5Test {
191+
192+
@Container
193+
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
194+
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
195+
.withExtension("JSON Body Transformer", Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
196+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "9cookies-wiremock-extensions.jar").toFile()));
197+
198+
@Test
199+
public void testJSONBodyTransformer() throws Exception {
200+
final HttpClient client = HttpClient.newBuilder().build();
201+
final HttpRequest request = HttpRequest.newBuilder()
202+
.uri(wiremockServer.getRequestURI("json-body-transformer"))
203+
.timeout(Duration.ofSeconds(10))
204+
.header("Content-Type", "application/json")
205+
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
206+
207+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
208+
209+
assertThat(response.body()).as("Wrong response body")
210+
.contains("Hello, John Doe!");
211+
}
212+
}
213+
```
214+
215+
##### Sample code using JUnit 4
216+
143217
```java
144218
public class WireMockContainerExtensionTest {
145219
@Rule
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)