|
| 1 | +package usecases; |
| 2 | + |
| 3 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 4 | +import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor; |
| 5 | +import static com.github.tomakehurst.wiremock.client.WireMock.anyUrl; |
| 6 | +import static com.github.tomakehurst.wiremock.client.WireMock.get; |
| 7 | +import static org.assertj.core.api.Assertions.assertThat; |
| 8 | + |
| 9 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 10 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 11 | +import io.restassured.RestAssured; |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Order; |
| 14 | +import org.junit.jupiter.api.Test; |
| 15 | +import org.springframework.beans.factory.annotation.Value; |
| 16 | +import org.springframework.boot.test.context.SpringBootTest; |
| 17 | +import org.wiremock.spring.ConfigureWireMock; |
| 18 | +import org.wiremock.spring.EnableWireMock; |
| 19 | +import org.wiremock.spring.InjectWireMock; |
| 20 | + |
| 21 | +@SpringBootTest |
| 22 | +@EnableWireMock({ |
| 23 | + @ConfigureWireMock( |
| 24 | + name = "wm1", |
| 25 | + portProperties = "wm1.server.port", |
| 26 | + baseUrlProperties = "wm1.server.url", |
| 27 | + resetWireMockServer = false), |
| 28 | + @ConfigureWireMock( |
| 29 | + name = "wm2", |
| 30 | + portProperties = "wm2.server.port", |
| 31 | + baseUrlProperties = "wm2.server.url") |
| 32 | +}) |
| 33 | +class ResetWireMockDisabledBetweenTest { |
| 34 | + |
| 35 | + @InjectWireMock("wm1") |
| 36 | + private WireMockServer wm1; |
| 37 | + |
| 38 | + @Value("${wm1.server.port}") |
| 39 | + private int wm1Port; |
| 40 | + |
| 41 | + @Value("${wm1.server.url}") |
| 42 | + private String wm1Url; |
| 43 | + |
| 44 | + @InjectWireMock("wm2") |
| 45 | + private WireMockServer wm2; |
| 46 | + |
| 47 | + @Value("${wm2.server.port}") |
| 48 | + private int wm2Port; |
| 49 | + |
| 50 | + @Value("${wm2.server.url}") |
| 51 | + private String wm2Url; |
| 52 | + |
| 53 | + private WireMock wm1Client; |
| 54 | + |
| 55 | + private WireMock wm2Client; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + public void before() { |
| 59 | + this.wm1Client = WireMock.create().port(this.wm1Port).build(); |
| 60 | + this.wm2Client = WireMock.create().port(this.wm2Port).build(); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + @Order(1) |
| 65 | + void test1() { |
| 66 | + this.wm1Client.register( |
| 67 | + get("/wm1_configured_in_test1").willReturn(aResponse().withStatus(202))); |
| 68 | + this.wm2Client.register( |
| 69 | + get("/wm2_configured_in_test1").willReturn(aResponse().withStatus(202))); |
| 70 | + |
| 71 | + assertThat(this.wm1Client.find(anyRequestedFor(anyUrl()))).hasSize(0); |
| 72 | + assertThat(this.wm1Client.allStubMappings().getMappings()).hasSize(1); |
| 73 | + |
| 74 | + assertThat(this.wm2Client.find(anyRequestedFor(anyUrl()))).hasSize(0); |
| 75 | + assertThat(this.wm2Client.allStubMappings().getMappings()).hasSize(1); |
| 76 | + |
| 77 | + this.wm1Client.register( |
| 78 | + get("/wm1_configured_in_test1_again").willReturn(aResponse().withStatus(202))); |
| 79 | + |
| 80 | + RestAssured.when().get(this.wm1Url + "/wm1_configured_in_test1_again").then().statusCode(202); |
| 81 | + |
| 82 | + assertThat(this.wm1Client.find(anyRequestedFor(anyUrl()))).hasSize(1); |
| 83 | + assertThat(this.wm1Client.allStubMappings().getMappings()).hasSize(2); |
| 84 | + |
| 85 | + assertThat(this.wm2Client.find(anyRequestedFor(anyUrl()))).hasSize(0); |
| 86 | + assertThat(this.wm2Client.allStubMappings().getMappings()).hasSize(1); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + @Order(2) |
| 91 | + void test2() { |
| 92 | + assertThat(this.wm1Client.find(anyRequestedFor(anyUrl()))) |
| 93 | + .as("should not have been reset") |
| 94 | + .hasSize(1); |
| 95 | + assertThat(this.wm1Client.allStubMappings().getMappings()) |
| 96 | + .as("should not have been reset") |
| 97 | + .hasSize(2); |
| 98 | + |
| 99 | + assertThat(this.wm2Client.find(anyRequestedFor(anyUrl()))) |
| 100 | + .as("should have been reset") |
| 101 | + .hasSize(0); |
| 102 | + assertThat(this.wm2Client.allStubMappings().getMappings()) |
| 103 | + .as("should have been reset") |
| 104 | + .hasSize(0); |
| 105 | + } |
| 106 | +} |
0 commit comments