Skip to content

Commit 42f0aea

Browse files
committed
Document the external WireMock Extension example
1 parent d3567ec commit 42f0aea

File tree

2 files changed

+90
-11
lines changed

2 files changed

+90
-11
lines changed

README.md

Lines changed: 90 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,8 @@ public class WireMockContainerTest {
5050

5151
@Test
5252
public void helloWorld() throws Exception {
53-
final HttpClient client = HttpClient.newBuilder()
54-
.version(HttpClient.Version.HTTP_1_1).build();
55-
56-
HttpRequest request = HttpRequest.newBuilder()
53+
final HttpClient client = HttpClient.newBuilder().build();
54+
final HttpRequest request = HttpRequest.newBuilder()
5755
.uri(wiremockServer.getRequestURI("hello"))
5856
.timeout(Duration.ofSeconds(10))
5957
.header("Content-Type", "application/json")
@@ -69,16 +67,101 @@ public class WireMockContainerTest {
6967
}
7068
```
7169

72-
### Using extensions
70+
### Using WireMock extensions
7371

7472
The API supports adding [WireMock extensions](https://wiremock.org/docs/extending-wiremock/)
7573
to the test container.
7674
The extension can be sourced from the classpath for bundled extensions,
7775
or added from the JAR file in the initializer.
7876

79-
#### Using external extension
77+
#### Using external extensions
78+
79+
For the external extensions,
80+
an extension Jar should be pulled to the test directory before running the test.
81+
[Apache Maven Dependency Plugin](https://maven.apache.org/plugins/maven-dependency-plugin/) can be used for this purpose.
82+
Make sure that all dependencies of the extension JAR, if any,
83+
are also included.
84+
85+
Below you can see an examples of using the _JSON Body Transformer_ extension
86+
from the [9cookies/wiremock-extensions](https://github.com/9cookies/wiremock-extensions).
87+
88+
Copying the dependency:
89+
90+
```xml
91+
<plugin>
92+
<groupId>org.apache.maven.plugins</groupId>
93+
<artifactId>maven-dependency-plugin</artifactId>
94+
<version>3.5.0</version>
95+
<executions>
96+
<execution>
97+
<id>copy</id>
98+
<phase>package</phase>
99+
<goals>
100+
<goal>copy</goal>
101+
</goals>
102+
<configuration>
103+
<artifactItems>
104+
<artifactItem>
105+
<groupId>com.ninecookies.wiremock.extensions</groupId>
106+
<artifactId>wiremock-extensions</artifactId>
107+
<version>0.4.1</version>
108+
<classifier>jar-with-dependencies</classifier>
109+
</artifactItem>
110+
</artifactItems>
111+
<outputDirectory>${project.build.directory}/test-wiremock-extension</outputDirectory>
112+
</configuration>
113+
</execution>
114+
</executions>
115+
</plugin>
116+
```
117+
118+
Mapping definition:
119+
120+
```json
121+
{
122+
"request": {
123+
"method": "POST",
124+
"url": "/json-body-transformer"
125+
},
126+
"response": {
127+
"status": 201,
128+
"headers": {
129+
"content-type": "application/json"
130+
},
131+
"jsonBody": {
132+
"message": "Hello, $(name)!"
133+
},
134+
"transformers" : ["json-body-transformer"]
135+
}
136+
}
137+
```
138+
139+
Test sample:
80140

81-
For the , an extension Jar should be
141+
```java
142+
public class WireMockContainerExtensionTest {
143+
@Rule
144+
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
145+
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
146+
.withExtension("JSON Body Transformer", Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
147+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "9cookies-wiremock-extensions.jar").toFile()));
148+
149+
@Test
150+
public void testJSONBodyTransformer() throws Exception {
151+
final HttpClient client = HttpClient.newBuilder().build();
152+
final HttpRequest request = HttpRequest.newBuilder()
153+
.uri(wiremockServer.getRequestURI("json-body-transformer"))
154+
.timeout(Duration.ofSeconds(10))
155+
.header("Content-Type", "application/json")
156+
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
157+
158+
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
159+
160+
assertThat(response.body()).as("Wrong response body")
161+
.contains("Hello, John Doe!");
162+
}
163+
}
164+
```
82165

83166
## Contributing
84167

pom.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,13 +123,9 @@
123123
<artifactId>wiremock-extensions</artifactId>
124124
<version>0.4.1</version>
125125
<classifier>jar-with-dependencies</classifier>
126-
<overWrite>false</overWrite>
127-
<destFileName>9cookies-wiremock-extensions.jar</destFileName>
128126
</artifactItem>
129127
</artifactItems>
130128
<outputDirectory>${project.build.directory}/test-wiremock-extension</outputDirectory>
131-
<overWriteReleases>false</overWriteReleases>
132-
<overWriteSnapshots>true</overWriteSnapshots>
133129
</configuration>
134130
</execution>
135131
</executions>

0 commit comments

Comments
 (0)