Skip to content

Commit 50b66ce

Browse files
authored
Merge pull request #71 from wiremock/feature/issue-70-optionally-disable-reset
feat: optionally disable reset to enable concurrency (refs #70)
2 parents 3c44d52 + 0b41641 commit 50b66ce

File tree

3 files changed

+135
-2
lines changed

3 files changed

+135
-2
lines changed

src/main/java/org/wiremock/spring/ConfigureWireMock.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,14 @@
115115
* @return the configuration customizers classes
116116
*/
117117
Class<? extends WireMockConfigurationCustomizer>[] configurationCustomizers() default {};
118+
119+
/**
120+
* When tests are running concurrently they will break each other if servers are being reset
121+
* between tests. Automatic reset is turned on by default, this option allows a user to turn it
122+
* off.
123+
*
124+
* @return true if {@link WireMockServer} should be invoked with {@link WireMockServer#resetAll()}
125+
* between test runs.
126+
*/
127+
boolean resetWireMockServer() default true;
118128
}

src/main/java/org/wiremock/spring/internal/WireMockSpringJunitExtension.java

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,32 @@ public class WireMockSpringJunitExtension
3131

3232
@Override
3333
public void beforeEach(final ExtensionContext extensionContext) throws Exception {
34-
// reset all wiremock servers associated with application context
35-
Store.INSTANCE.findAllInstances(extensionContext).forEach(WireMockServer::resetAll);
34+
this.resetWireMockServersIfConfigured(extensionContext);
3635

3736
// inject properties into test class fields
3837
injectWireMockInstances(extensionContext, InjectWireMock.class, InjectWireMock::value);
3938

4039
this.configureWireMockForDefaultInstance(extensionContext);
4140
}
4241

42+
private void resetWireMockServersIfConfigured(final ExtensionContext extensionContext) {
43+
final List<Object> instances = extensionContext.getRequiredTestInstances().getAllInstances();
44+
for (final Object instance : instances) {
45+
final EnableWireMock enableWireMockAnnotation =
46+
AnnotationUtils.findAnnotation(instance.getClass(), EnableWireMock.class);
47+
if (enableWireMockAnnotation == null) {
48+
continue;
49+
}
50+
final ConfigureWireMock[] wireMockServers =
51+
WireMockContextCustomizerFactory.getConfigureWireMocksOrDefault(
52+
enableWireMockAnnotation.value());
53+
List.of(wireMockServers).stream()
54+
.filter(it -> it.resetWireMockServer())
55+
.map(it -> Store.INSTANCE.findRequiredWireMockInstance(extensionContext, it.name()))
56+
.forEach(WireMockServer::resetAll);
57+
}
58+
}
59+
4360
private void configureWireMockForDefaultInstance(final ExtensionContext extensionContext) {
4461
final List<Object> instances = extensionContext.getRequiredTestInstances().getAllInstances();
4562
WireMockServer wiremock = null;
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
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

Comments
 (0)