From f2c35d0bc7e22fd230b4d94192e36b39b2ef45fb Mon Sep 17 00:00:00 2001 From: Tomas Bjerre Date: Sun, 6 Oct 2024 09:12:39 +0200 Subject: [PATCH] chore: ResetWireMockBetweenTest --- .git-blame-ignore-revs | 2 + build.gradle | 2 +- gradle.properties | 17 +-- .../java/app/ResetWireMockBetweenTest.java | 103 ++++++++++++++++++ 4 files changed, 115 insertions(+), 9 deletions(-) create mode 100644 .git-blame-ignore-revs create mode 100644 wiremock-spring-boot-example/src/test/java/app/ResetWireMockBetweenTest.java diff --git a/.git-blame-ignore-revs b/.git-blame-ignore-revs new file mode 100644 index 0000000..b69fdec --- /dev/null +++ b/.git-blame-ignore-revs @@ -0,0 +1,2 @@ +5870e1ef80feb27342e0e8b1fd5369cb21a38e57 + diff --git a/build.gradle b/build.gradle index ca74a2d..91eaf6c 100644 --- a/build.gradle +++ b/build.gradle @@ -32,7 +32,7 @@ dependencies { api "org.springframework.boot:spring-boot-test:3.3.4" api "org.springframework:spring-test:6.1.13" api "org.slf4j:slf4j-api:2.0.16" - api "org.junit.jupiter:junit-jupiter-api:5.11.0" + api 'org.junit.jupiter:junit-jupiter-api:5.11.2' } project('wiremock-spring-boot-example', { diff --git a/gradle.properties b/gradle.properties index d8fa88c..2062723 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,14 +1,15 @@ -#Wed Sep 25 18:22:00 CEST 2024 +# +#Sun Oct 06 08:21:20 CEST 2024 description=WireMock integration for Spring Boot. -version=3.0.0-SNAPSHOT group=org.wiremock.spring -sourceCompatibility=17 -targetCompatibility=17 -website=https://github.com/wiremock/wiremock-spring-boot -vcsUrl=https://github.com/wiremock/wiremock-spring-boot licenseName=MIT -nexusUsernameEnvOrProp=OSSRH_USERNAME nexusPasswordEnvOrProp=OSSRH_TOKEN +nexusUsernameEnvOrProp=OSSRH_USERNAME signingKeyEnvOrProp=OSSRH_GPG_SECRET_KEY signingPasswordEnvOrProp=OSSRH_GPG_SECRET_KEY_PASSWORD -tags=wiremock,springboot \ No newline at end of file +sourceCompatibility=17 +tags=wiremock,springboot +targetCompatibility=17 +vcsUrl=https\://github.com/wiremock/wiremock-spring-boot +version=3.0.0 +website=https\://github.com/wiremock/wiremock-spring-boot diff --git a/wiremock-spring-boot-example/src/test/java/app/ResetWireMockBetweenTest.java b/wiremock-spring-boot-example/src/test/java/app/ResetWireMockBetweenTest.java new file mode 100644 index 0000000..fb76074 --- /dev/null +++ b/wiremock-spring-boot-example/src/test/java/app/ResetWireMockBetweenTest.java @@ -0,0 +1,103 @@ +package app; + +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; +import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor; +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; +import static com.github.tomakehurst.wiremock.client.WireMock.get; +import static org.assertj.core.api.Assertions.assertThat; + +import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.client.WireMock; +import io.restassured.RestAssured; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.test.context.SpringBootTest; +import org.wiremock.spring.ConfigureWireMock; +import org.wiremock.spring.EnableWireMock; +import org.wiremock.spring.InjectWireMock; + +@SpringBootTest +@EnableWireMock({ + @ConfigureWireMock( + name = "wm1", + portProperties = "wm1.server.port", + baseUrlProperties = "wm1.server.url"), + @ConfigureWireMock( + name = "wm2", + portProperties = "wm2.server.port", + baseUrlProperties = "wm2.server.url") +}) +class ResetWireMockBetweenTest { + + @InjectWireMock("wm1") + private WireMockServer wm1; + + @Value("${wm1.server.port}") + private int wm1Port; + + @Value("${wm1.server.url}") + private String wm1Url; + + @InjectWireMock("wm2") + private WireMockServer wm2; + + @Value("${wm2.server.port}") + private int wm2Port; + + @Value("${wm2.server.url}") + private String wm2Url; + + private WireMock wm1Client; + + private WireMock wm2Client; + + @BeforeEach + public void before() { + this.wm1Client = WireMock.create().port(this.wm1Port).build(); + this.wm1Client.register( + get("/wm1_configured_in_before").willReturn(aResponse().withStatus(202))); + + this.wm2Client = WireMock.create().port(this.wm2Port).build(); + this.wm2Client.register( + get("/wm2_configured_in_before").willReturn(aResponse().withStatus(202))); + } + + @Test + void test1() { + assertThat(this.wm1Client.find(anyRequestedFor(anyUrl()))).hasSize(0); + assertThat(this.wm1Client.allStubMappings().getMappings()).hasSize(1); + + assertThat(this.wm2Client.find(anyRequestedFor(anyUrl()))).hasSize(0); + assertThat(this.wm2Client.allStubMappings().getMappings()).hasSize(1); + + this.wm1Client.register(get("/wm1_configured_in_test").willReturn(aResponse().withStatus(202))); + + RestAssured.when().get(this.wm1Url + "/wm1_configured_in_test").then().statusCode(202); + + assertThat(this.wm1Client.find(anyRequestedFor(anyUrl()))).hasSize(1); + assertThat(this.wm1Client.allStubMappings().getMappings()).hasSize(2); + + assertThat(this.wm2Client.find(anyRequestedFor(anyUrl()))).hasSize(0); + assertThat(this.wm2Client.allStubMappings().getMappings()).hasSize(1); + } + + @Test + void test2() { + assertThat(this.wm1Client.find(anyRequestedFor(anyUrl()))).hasSize(0); + assertThat(this.wm1Client.allStubMappings().getMappings()).hasSize(1); + + assertThat(this.wm2Client.find(anyRequestedFor(anyUrl()))).hasSize(0); + assertThat(this.wm2Client.allStubMappings().getMappings()).hasSize(1); + + this.wm2Client.register(get("/wm2_configured_in_test").willReturn(aResponse().withStatus(202))); + + RestAssured.when().get(this.wm2Url + "/wm2_configured_in_test").then().statusCode(202); + + assertThat(this.wm1Client.find(anyRequestedFor(anyUrl()))).hasSize(0); + assertThat(this.wm1Client.allStubMappings().getMappings()).hasSize(1); + + assertThat(this.wm2Client.find(anyRequestedFor(anyUrl()))).hasSize(1); + assertThat(this.wm2Client.allStubMappings().getMappings()).hasSize(2); + } +}