Restarting WireMock stubs when reusing servers running on random ports
This commit is contained in:
@@ -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)) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user