Skip to content

Commit

Permalink
chore: ResetWireMockBetweenTest
Browse files Browse the repository at this point in the history
  • Loading branch information
tomasbjerre committed Oct 6, 2024
1 parent 72bd586 commit f2c35d0
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
5870e1ef80feb27342e0e8b1fd5369cb21a38e57

2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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', {
Expand Down
17 changes: 9 additions & 8 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -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
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
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit f2c35d0

Please sign in to comment.