@@ -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.* ;
4743import 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 ;
8679import 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}
0 commit comments