Not causing an exception when context is broken; fixes gh-833

This commit is contained in:
Marcin Grzejszczak
2019-01-18 16:20:22 +01:00
parent f3d19c78ad
commit 4619d81217

View File

@@ -27,8 +27,12 @@ public final class StubRunnerWireMockTestExecutionListener extends AbstractTestE
private static Map<ApplicationContext, Map<WireMockHttpServerStub, PortAndMappings>> STUBS = new ConcurrentHashMap<>();
@Override public void beforeTestClass(TestContext testContext) {
ApplicationContext context = context(testContext);
if (context == null) {
return;
}
Map<WireMockHttpServerStub, PortAndMappings> stubs = STUBS
.get(testContext.getApplicationContext());
.get(context);
if (stubs != null) {
if (log.isDebugEnabled()) {
log.debug("Found a matching application context from [" + testContext.getTestClass().getName() + "]");
@@ -60,7 +64,11 @@ public final class StubRunnerWireMockTestExecutionListener extends AbstractTestE
}
@Override public void afterTestClass(TestContext testContext) {
STUBS.put(testContext.getApplicationContext(), WireMockHttpServerStub.SERVERS);
ApplicationContext context = context(testContext);
if (context == null) {
return;
}
STUBS.put(context, WireMockHttpServerStub.SERVERS);
if (log.isDebugEnabled()) {
log.debug("Stopping servers " + WireMockHttpServerStub.SERVERS);
}
@@ -68,4 +76,15 @@ public final class StubRunnerWireMockTestExecutionListener extends AbstractTestE
serverStub.stop();
}
}
private ApplicationContext context(TestContext testContext) {
try {
return testContext.getApplicationContext();
} catch (Exception ex) {
if (log.isDebugEnabled() ) {
log.debug("Exception occurred while trying to retrieve the application context", ex);
}
return null;
}
}
}