Properly close context hierarchy in tests

Prior to this commit, some tests were creating a parent/child
relationship but were only closing the child context. This could
be an issue with the autoconfig module as a lot of auto-config
kicks in by default.

This commit adds a new test utility designed to properly handle
those situations. Updated tests that were creating a context
hierarchy to benefit from that.

Fixes gh-1034
This commit is contained in:
Stephane Nicoll
2014-06-11 14:45:39 +02:00
committed by Dave Syer
parent f83395b40c
commit b646231327
6 changed files with 112 additions and 17 deletions

View File

@@ -20,7 +20,7 @@ import org.junit.After;
import org.junit.Test;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.context.ApplicationContext;
import org.springframework.boot.test.ApplicationContextTestUtils;
import org.springframework.context.ConfigurableApplicationContext;
/**
@@ -34,13 +34,7 @@ public class SpringApplicationHierarchyTests {
@After
public void after() {
if (this.context != null) {
ApplicationContext parentContext = this.context.getParent();
if (parentContext instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) parentContext).close();
}
this.context.close();
}
ApplicationContextTestUtils.closeAll(this.context);
}
@Test

View File

@@ -23,6 +23,7 @@ import org.junit.After;
import org.junit.Test;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.test.ApplicationContextTestUtils;
import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
@@ -44,9 +45,7 @@ public class ShutdownParentEndpointTests {
@After
public void close() {
if (this.context != null) {
this.context.close();
}
ApplicationContextTestUtils.closeAll(this.context);
}
@Test