Commit b6462313 authored by Stephane Nicoll's avatar Stephane Nicoll Committed by Dave Syer

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
parent f83395b4
......@@ -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
......
......@@ -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
......
......@@ -27,6 +27,7 @@ import org.junit.Test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.test.ApplicationContextTestUtils;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.http.MediaType;
......@@ -55,9 +56,7 @@ public class BasicErrorControllerDirectMockMvcTests {
@After
public void close() {
if (this.wac != null) {
this.wac.close();
}
ApplicationContextTestUtils.closeAll(this.wac);
}
public void setup(ConfigurableWebApplicationContext context) {
......
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
/**
* Application context related test utilities.
*
* @author Stephane Nicoll
* @since 1.1.1
*/
public abstract class ApplicationContextTestUtils {
/**
* Closes this {@link ApplicationContext} and its parent hierarchy
* if any.
* @param context the context to close (can be {@code null})
*/
public static void closeAll(ApplicationContext context) {
if (context != null) {
ApplicationContext parent = context.getParent();
if (context instanceof ConfigurableApplicationContext) {
((ConfigurableApplicationContext) context).close();
}
closeAll(parent);
}
}
}
......@@ -22,6 +22,8 @@ import java.util.Collections;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.test.ApplicationContextTestUtils;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
......@@ -52,9 +54,7 @@ public class SpringApplicationBuilderTests {
@After
public void close() {
if (this.context != null) {
this.context.close();
}
ApplicationContextTestUtils.closeAll(this.context);
}
@Test
......
/*
* Copyright 2012-2014 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.test;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
/**
*
* @author Stephane Nicoll
*/
public class ApplicationContextTestUtilsTests {
@Test
public void closeNull() {
ApplicationContextTestUtils.closeAll(null);
}
@Test
public void closeNonClosableContext() {
ApplicationContext mock = mock(ApplicationContext.class);
ApplicationContextTestUtils.closeAll(mock);
}
@Test
public void closeContextAndParent() {
ConfigurableApplicationContext mock = mock(ConfigurableApplicationContext.class);
ConfigurableApplicationContext parent = mock(ConfigurableApplicationContext.class);
given(mock.getParent()).willReturn(parent);
given(parent.getParent()).willReturn(null);
ApplicationContextTestUtils.closeAll(mock);
verify(mock).getParent();
verify(mock).close();
verify(parent).getParent();
verify(parent).close();
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment