Skip to content

Commit

Permalink
Merge pull request #71 from wiremock/feature/issue-70-optionally-disa…
Browse files Browse the repository at this point in the history
…ble-reset

feat: optionally disable reset to enable concurrency (refs #70)
  • Loading branch information
tomasbjerre authored Dec 8, 2024
2 parents 3c44d52 + 0b41641 commit 50b66ce
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 2 deletions.
10 changes: 10 additions & 0 deletions src/main/java/org/wiremock/spring/ConfigureWireMock.java
Original file line number Diff line number Diff line change
Expand Up @@ -115,4 +115,14 @@
* @return the configuration customizers classes
*/
Class<? extends WireMockConfigurationCustomizer>[] configurationCustomizers() default {};

/**
* When tests are running concurrently they will break each other if servers are being reset
* between tests. Automatic reset is turned on by default, this option allows a user to turn it
* off.
*
* @return true if {@link WireMockServer} should be invoked with {@link WireMockServer#resetAll()}
* between test runs.
*/
boolean resetWireMockServer() default true;
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,32 @@ public class WireMockSpringJunitExtension

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

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

this.configureWireMockForDefaultInstance(extensionContext);
}

private void resetWireMockServersIfConfigured(final ExtensionContext extensionContext) {
final List<Object> instances = extensionContext.getRequiredTestInstances().getAllInstances();
for (final Object instance : instances) {
final EnableWireMock enableWireMockAnnotation =
AnnotationUtils.findAnnotation(instance.getClass(), EnableWireMock.class);
if (enableWireMockAnnotation == null) {
continue;
}
final ConfigureWireMock[] wireMockServers =
WireMockContextCustomizerFactory.getConfigureWireMocksOrDefault(
enableWireMockAnnotation.value());
List.of(wireMockServers).stream()
.filter(it -> it.resetWireMockServer())
.map(it -> Store.INSTANCE.findRequiredWireMockInstance(extensionContext, it.name()))
.forEach(WireMockServer::resetAll);
}
}

private void configureWireMockForDefaultInstance(final ExtensionContext extensionContext) {
final List<Object> instances = extensionContext.getRequiredTestInstances().getAllInstances();
WireMockServer wiremock = null;
Expand Down
106 changes: 106 additions & 0 deletions src/test/java/usecases/ResetWireMockDisabledBetweenTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package usecases;

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.Order;
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",
resetWireMockServer = false),
@ConfigureWireMock(
name = "wm2",
portProperties = "wm2.server.port",
baseUrlProperties = "wm2.server.url")
})
class ResetWireMockDisabledBetweenTest {

@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.wm2Client = WireMock.create().port(this.wm2Port).build();
}

@Test
@Order(1)
void test1() {
this.wm1Client.register(
get("/wm1_configured_in_test1").willReturn(aResponse().withStatus(202)));
this.wm2Client.register(
get("/wm2_configured_in_test1").willReturn(aResponse().withStatus(202)));

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_test1_again").willReturn(aResponse().withStatus(202)));

RestAssured.when().get(this.wm1Url + "/wm1_configured_in_test1_again").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
@Order(2)
void test2() {
assertThat(this.wm1Client.find(anyRequestedFor(anyUrl())))
.as("should not have been reset")
.hasSize(1);
assertThat(this.wm1Client.allStubMappings().getMappings())
.as("should not have been reset")
.hasSize(2);

assertThat(this.wm2Client.find(anyRequestedFor(anyUrl())))
.as("should have been reset")
.hasSize(0);
assertThat(this.wm2Client.allStubMappings().getMappings())
.as("should have been reset")
.hasSize(0);
}
}

0 comments on commit 50b66ce

Please sign in to comment.