|
| 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