Skip to content

Commit 8a89639

Browse files
Merge pull request #49 from bitxon/feature/junit-5-as-base-framework
Use Junit5 as base test framework
2 parents 9db2cb0 + dfdd22a commit 8a89639

9 files changed

+274
-240
lines changed

README.md

Lines changed: 85 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -39,38 +39,32 @@ P.S: Javadoc is coming soon!
3939
#### Sample Code using JUnit 5
4040

4141
```java
42-
import static org.assertj.core.api.Assertions.assertThat;
43-
44-
import java.net.http.*;
45-
import java.time.Duration;
46-
import org.junit.jupiter.api.Test;
42+
import org.junit.jupiter.api.*;
4743
import org.testcontainers.junit.jupiter.*;
44+
import org.wiremock.integrations.testcontainers.testsupport.http.*;
45+
46+
import static org.assertj.core.api.Assertions.assertThat;
4847

4948
@Testcontainers
50-
public class WireMockContainerJUnit5Test {
51-
52-
@Container
53-
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
54-
.withMapping("hello", WireMockContainerTest.class, "hello-world.json")
55-
.withMapping("hello-resource", WireMockContainerTest.class, "hello-world-resource.json")
56-
.withFileFromResource("hello-world-resource-response.xml", WireMockContainerTest.class,
57-
"hello-world-resource-response.xml");
58-
59-
@Test
60-
public void helloWorld() throws Exception {
61-
final HttpClient client = HttpClient.newBuilder().build();
62-
final HttpRequest request = HttpRequest.newBuilder()
63-
.uri(URI.create(wiremockServer.getUrl("/hello")))
64-
.timeout(Duration.ofSeconds(10))
65-
.header("Content-Type", "application/json")
66-
.GET().build();
67-
68-
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
69-
70-
assertThat(response.body())
71-
.as("Wrong response body")
72-
.contains("Hello, world!");
73-
}
49+
class WireMockContainerJunit5Test {
50+
51+
@Container
52+
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
53+
.withMapping("hello", WireMockContainerJunit5Test.class, "hello-world.json");
54+
55+
@Test
56+
void helloWorld() throws Exception {
57+
// given
58+
String url = wiremockServer.getUrl("/hello");
59+
60+
// when
61+
HttpResponse response = new TestHttpClient().get(url);
62+
63+
// then
64+
assertThat(response.getBody())
65+
.as("Wrong response body")
66+
.contains("Hello, world!");
67+
}
7468
}
7569
```
7670

@@ -82,31 +76,27 @@ Show Code
8276
</summary>
8377

8478
```java
85-
import org.wiremock.integrations.testcontainers.WireMockContainer;
8679
import org.junit.*;
87-
import java.net.URI;
88-
import java.net.http.*;
89-
import java.time.Duration;
80+
import org.wiremock.integrations.testcontainers.testsupport.http.*;
9081

91-
public class WireMockContainerTest {
82+
import static org.assertj.core.api.Assertions.assertThat;
83+
84+
public class WireMockContainerJunit4Test {
9285

9386
@Rule
9487
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
95-
.withMapping("hello", WireMockContainerTest.class, "hello-world.json");
88+
.withMapping("hello", WireMockContainerJunit4Test.class, "hello-world.json");
9689

9790
@Test
9891
public void helloWorld() throws Exception {
99-
final HttpClient client = HttpClient.newBuilder().build();
100-
final HttpRequest request = HttpRequest.newBuilder()
101-
.uri(URI.create(wiremockServer.getUrl("/hello")))
102-
.timeout(Duration.ofSeconds(10))
103-
.header("Content-Type", "application/json")
104-
.GET().build();
92+
// given
93+
String url = wiremockServer.getUrl("/hello");
10594

106-
HttpResponse<String> response =
107-
client.send(request, HttpResponse.BodyHandlers.ofString());
95+
// when
96+
HttpResponse response = new TestHttpClient().get(url);
10897

109-
assertThat(response.body())
98+
// then
99+
assertThat(response.getBody())
110100
.as("Wrong response body")
111101
.contains("Hello, world!");
112102
}
@@ -188,29 +178,38 @@ Test sample:
188178
##### Sample code using JUnit 5
189179

190180
```java
181+
import org.junit.jupiter.api.*;
182+
import org.testcontainers.junit.jupiter.*;
183+
import org.wiremock.integrations.testcontainers.testsupport.http.*;
184+
185+
import java.nio.file.Paths;
186+
import java.util.Collections;
187+
188+
import static org.assertj.core.api.Assertions.assertThat;
189+
191190
@Testcontainers
192-
public class WireMockContainerExtensionJUnit5Test {
193-
194-
@Container
195-
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
196-
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
197-
.withExtension("JSON Body Transformer", Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
198-
Collections.singleton(Paths.get("target", "test-wiremock-extension", "9cookies-wiremock-extensions.jar").toFile()));
199-
200-
@Test
201-
public void testJSONBodyTransformer() throws Exception {
202-
final HttpClient client = HttpClient.newBuilder().build();
203-
final HttpRequest request = HttpRequest.newBuilder()
204-
.uri(URI.create(wiremockServer.getUrl("/json-body-transformer")))
205-
.timeout(Duration.ofSeconds(10))
206-
.header("Content-Type", "application/json")
207-
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
208-
209-
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
210-
211-
assertThat(response.body()).as("Wrong response body")
212-
.contains("Hello, John Doe!");
213-
}
191+
class WireMockContainerExtensionJunit5Test {
192+
193+
@Container
194+
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
195+
.withMapping("json-body-transformer", WireMockContainerExtensionJunit5Test.class, "json-body-transformer.json")
196+
.withExtension("JSON Body Transformer",
197+
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
198+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
199+
200+
@Test
201+
void testJSONBodyTransformer() throws Exception {
202+
// given
203+
String url = wiremockServer.getUrl("/json-body-transformer");
204+
String body = "{\"name\":\"John Doe\"}";
205+
206+
// when
207+
HttpResponse response = new TestHttpClient().post(url, body);
208+
209+
// then
210+
assertThat(response.getBody()).as("Wrong response body")
211+
.contains("Hello, John Doe!");
212+
}
214213
}
215214
```
216215

@@ -222,26 +221,34 @@ Show Code
222221
</summary>
223222

224223
```java
224+
import org.junit.*;
225+
import org.wiremock.integrations.testcontainers.testsupport.http.*;
226+
227+
import java.nio.file.Paths;
228+
import java.util.Collections;
229+
230+
import static org.assertj.core.api.Assertions.assertThat;
231+
232+
public class WireMockContainerExtensionJunit4Test {
225233

226-
public class WireMockContainerExtensionTest {
227234
@Rule
228235
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
229-
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
230-
.withExtension("JSON Body Transformer", Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
231-
Collections.singleton(Paths.get("target", "test-wiremock-extension", "9cookies-wiremock-extensions.jar").toFile()));
236+
.withMapping("json-body-transformer", WireMockContainerExtensionJunit4Test.class, "json-body-transformer.json")
237+
.withExtension("JSON Body Transformer",
238+
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
239+
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
232240

233241
@Test
234242
public void testJSONBodyTransformer() throws Exception {
235-
final HttpClient client = HttpClient.newBuilder().build();
236-
final HttpRequest request = HttpRequest.newBuilder()
237-
.uri(URI.create(wiremockServer.getUrl("/json-body-transformer")))
238-
.timeout(Duration.ofSeconds(10))
239-
.header("Content-Type", "application/json")
240-
.POST(HttpRequest.BodyPublishers.ofString("{\"name\":\"John Doe\"}")).build();
243+
// given
244+
String url = wiremockServer.getUrl("/json-body-transformer");
245+
String body = "{\"name\":\"John Doe\"}";
241246

242-
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
247+
// when
248+
HttpResponse response = new TestHttpClient().post(url, body);
243249

244-
assertThat(response.body()).as("Wrong response body")
250+
// then
251+
assertThat(response.getBody()).as("Wrong response body")
245252
.contains("Hello, John Doe!");
246253
}
247254
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright (C) 2023 WireMock Inc, Oleg Nenashev and all project contributors
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.wiremock.integrations.testcontainers;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.assertj.core.api.Assertions.assertThat;
21+
22+
class WireMockContainerBannerTest {
23+
24+
WireMockContainer wireMockContainer = new WireMockContainer("2.35.0");
25+
26+
@Test
27+
void bannerIsByDefaultDisabled() {
28+
// when
29+
wireMockContainer.configure();
30+
31+
// then
32+
assertThat(wireMockContainer.getCommandParts())
33+
.contains("--disable-banner");
34+
}
35+
36+
@Test
37+
void enableBanner() {
38+
// given
39+
wireMockContainer.withBanner();
40+
41+
// when
42+
wireMockContainer.configure();
43+
44+
// then
45+
assertThat(wireMockContainer.getCommandParts())
46+
.doesNotContain("--disable-banner");
47+
}
48+
49+
@Test
50+
void disableBanner() {
51+
// given
52+
wireMockContainer.withoutBanner();
53+
54+
// when
55+
wireMockContainer.configure();
56+
57+
// then
58+
assertThat(wireMockContainer.getCommandParts())
59+
.contains("--disable-banner");
60+
}
61+
}

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionTest.java

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,12 @@
1515
*/
1616
package org.wiremock.integrations.testcontainers;
1717

18-
import org.junit.Before;
19-
import org.junit.Rule;
20-
import org.junit.Test;
18+
import org.junit.jupiter.api.Test;
2119
import org.slf4j.Logger;
2220
import org.slf4j.LoggerFactory;
2321
import org.testcontainers.containers.output.Slf4jLogConsumer;
22+
import org.testcontainers.junit.jupiter.Container;
23+
import org.testcontainers.junit.jupiter.Testcontainers;
2424
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
2525
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
2626

@@ -34,25 +34,22 @@
3434
* Tests the WireMock extension loading.
3535
* It uses the external Jar supplied by the Maven Dependency Plugin.
3636
*/
37-
public class WireMockContainerExtensionTest {
37+
@Testcontainers
38+
class WireMockContainerExtensionTest {
3839

39-
public static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionTest.class);
40+
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionTest.class);
4041

41-
@Rule
42-
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
42+
@Container
43+
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
44+
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
4345
.withStartupTimeout(Duration.ofSeconds(60))
4446
.withMapping("json-body-transformer", WireMockContainerExtensionTest.class, "json-body-transformer.json")
4547
.withExtension("JSON Body Transformer",
4648
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
4749
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
4850

49-
@Before
50-
public void before() {
51-
wiremockServer.followOutput(new Slf4jLogConsumer(LOGGER));
52-
}
53-
5451
@Test
55-
public void testJSONBodyTransformer() throws Exception {
52+
void testJSONBodyTransformer() throws Exception {
5653
// given
5754
String url = wiremockServer.getUrl("/json-body-transformer");
5855
String body = "{\"name\":\"John Doe\"}";

src/test/java/org/wiremock/integrations/testcontainers/WireMockContainerExtensionsCombinationTest.java

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,31 +15,32 @@
1515
*/
1616
package org.wiremock.integrations.testcontainers;
1717

18-
import org.junit.Before;
19-
import org.junit.Rule;
20-
import org.junit.Test;
18+
import org.junit.jupiter.api.Test;
2119
import org.slf4j.Logger;
2220
import org.slf4j.LoggerFactory;
2321
import org.testcontainers.containers.output.Slf4jLogConsumer;
22+
import org.testcontainers.junit.jupiter.Container;
23+
import org.testcontainers.junit.jupiter.Testcontainers;
2424
import org.wiremock.integrations.testcontainers.testsupport.http.HttpResponse;
2525
import org.wiremock.integrations.testcontainers.testsupport.http.TestHttpClient;
2626

2727
import java.nio.file.Paths;
2828
import java.util.Collections;
29-
import java.util.concurrent.TimeUnit;
3029

3130
import static org.assertj.core.api.Assertions.assertThat;
3231

3332
/**
3433
* Tests the WireMock extension loading.
3534
* It uses multiple external Jars supplied by the Maven Dependency Plugin.
3635
*/
37-
public class WireMockContainerExtensionsCombinationTest {
36+
@Testcontainers
37+
class WireMockContainerExtensionsCombinationTest {
3838

39-
public static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsCombinationTest.class);
39+
private static final Logger LOGGER = LoggerFactory.getLogger(WireMockContainerExtensionsCombinationTest.class);
4040

41-
@Rule
42-
public WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
41+
@Container
42+
WireMockContainer wiremockServer = new WireMockContainer("2.35.0")
43+
.withLogConsumer(new Slf4jLogConsumer(LOGGER))
4344
.withMapping("json-body-transformer", WireMockContainerExtensionsCombinationTest.class, "json-body-transformer.json")
4445
.withExtension("Webhook",
4546
Collections.singleton("org.wiremock.webhooks.Webhooks"),
@@ -48,13 +49,8 @@ public class WireMockContainerExtensionsCombinationTest {
4849
Collections.singleton("com.ninecookies.wiremock.extensions.JsonBodyTransformer"),
4950
Collections.singleton(Paths.get("target", "test-wiremock-extension", "wiremock-extensions-0.4.1-jar-with-dependencies.jar").toFile()));
5051

51-
@Before
52-
public void before() {
53-
wiremockServer.followOutput(new Slf4jLogConsumer(LOGGER));
54-
}
55-
5652
@Test
57-
public void testJSONBodyTransformer() throws Exception {
53+
void testJSONBodyTransformer() throws Exception {
5854
// given
5955
String url = wiremockServer.getUrl("/json-body-transformer");
6056
String body = "{\"name\":\"John Doe\"}";

0 commit comments

Comments
 (0)