Restarting WireMock stubs when reusing servers running on random ports

This commit is contained in:
Marcin Grzejszczak
2019-02-18 10:17:45 +01:00
parent d7f39c1a80
commit b733a718a4
2 changed files with 27 additions and 0 deletions

View File

@@ -118,6 +118,10 @@ public class WireMockConfiguration implements SmartLifecycle {
}
}
void resetMappings() {
this.server.resetToDefaultMappings();
}
private void registerStubs() throws IOException {
for (String stubs : this.wireMock.getServer().getStubs()) {
if (StringUtils.hasText(stubs)) {

View File

@@ -35,6 +35,21 @@ public final class WireMockTestExecutionListener extends AbstractTestExecutionLi
private static final Log log = LogFactory.getLog(WireMockTestExecutionListener.class);
@Override
public void beforeTestClass(TestContext testContext) throws Exception {
if (applicationContextBroken(testContext)
|| wireMockConfigurationMissing(testContext)
|| annotationMissing(testContext)) {
return;
}
if (!portIsFixed(testContext)) {
if (log.isDebugEnabled()) {
log.debug("Re-registering default mappings");
}
wireMockConfig(testContext).init();
}
}
@Override
public void afterTestClass(TestContext testContext) {
if (applicationContextBroken(testContext)
@@ -52,6 +67,14 @@ public final class WireMockTestExecutionListener extends AbstractTestExecutionLi
testContext
.markApplicationContextDirty(DirtiesContext.HierarchyMode.EXHAUSTIVE);
}
else {
if (log.isDebugEnabled()) {
log.debug(
"Resetting mappings for the next test to restart them. That's necessary when"
+ "reusing the same context with new servers running on random ports");
}
wireMockConfig(testContext).resetMappings();
}
}
private boolean annotationMissing(TestContext testContext) {