From 7783e90fa5d1b4905c3381f5a002ea0b39925649 Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Thu, 10 Oct 2024 18:23:41 +0200 Subject: [PATCH] testing #48 --- .../__files/second-server/200_response.json | 1 + .../src/test/java/app/BodyFileTest.java | 51 +++++++++++++++++++ 2 files changed, 52 insertions(+) create mode 100644 wiremock-spring-boot-example/src/test/body-file/__files/second-server/200_response.json create mode 100644 wiremock-spring-boot-example/src/test/java/app/BodyFileTest.java diff --git a/wiremock-spring-boot-example/src/test/body-file/__files/second-server/200_response.json b/wiremock-spring-boot-example/src/test/body-file/__files/second-server/200_response.json new file mode 100644 index 0000000..89a6d8b --- /dev/null +++ b/wiremock-spring-boot-example/src/test/body-file/__files/second-server/200_response.json @@ -0,0 +1 @@ +{"response":"consent response"} \ No newline at end of file diff --git a/wiremock-spring-boot-example/src/test/java/app/BodyFileTest.java b/wiremock-spring-boot-example/src/test/java/app/BodyFileTest.java new file mode 100644 index 0000000..6e08ae7 --- /dev/null +++ b/wiremock-spring-boot-example/src/test/java/app/BodyFileTest.java @@ -0,0 +1,51 @@ +package app; + +import static org.assertj.core.api.Assertions.assertThat; + +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpHeaders; +import org.springframework.http.MediaType; +import org.springframework.test.context.TestPropertySource; +import org.wiremock.spring.ConfigureWireMock; +import org.wiremock.spring.EnableWireMock; +import org.wiremock.spring.InjectWireMock; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; + +import io.restassured.RestAssured; + +@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +@TestPropertySource(properties = { + "application.client.first-server.token.uri=${wiremock.server.first-server.baseUrl}/auth/realms/test-realm/protocol/openid-connect/token", + "application.client.second-server.uri=${wiremock.server.second-server.baseUrl}" }) +@EnableWireMock({ + @ConfigureWireMock(name = "first-server", baseUrlProperties = { "wiremock.server.first-server.baseUrl" }), + @ConfigureWireMock(name = "second-server", baseUrlProperties = { + "wiremock.server.second-server.baseUrl" }, filesUnderDirectory = "src/test/body-file") }) +public class BodyFileTest { + + @InjectWireMock("second-server") + private WireMockServer secondServer; + + @Test + public void testCorrectResponseFromServer() { + this.secondServer.stubFor(WireMock.post("/consent") + .willReturn(WireMock.ok().withHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE) + .withBodyFile("second-server/200_response.json"))); + + RestAssured.baseURI = "http://localhost:" + this.secondServer.port(); + final String actual = RestAssured.when().post("/consent").then().log().all().statusCode(200).extract() + .asPrettyString(); + + assertThat(actual).isEqualToIgnoringWhitespace(""" + { + "response": "consent response" + } + """); + } + +}