From b64623132745769d604c8efdd936a997035eedff Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Wed, 11 Jun 2014 14:45:39 +0200 Subject: [PATCH] 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 --- .../SpringApplicationHierarchyTests.java | 10 +--- .../endpoint/ShutdownParentEndpointTests.java | 5 +- ...asicErrorControllerDirectMockMvcTests.java | 5 +- .../test/ApplicationContextTestUtils.java | 46 +++++++++++++++ .../SpringApplicationBuilderTests.java | 6 +- .../ApplicationContextTestUtilsTests.java | 57 +++++++++++++++++++ 6 files changed, 112 insertions(+), 17 deletions(-) create mode 100644 spring-boot/src/main/java/org/springframework/boot/test/ApplicationContextTestUtils.java create mode 100644 spring-boot/src/test/java/org/springframework/boot/test/ApplicationContextTestUtilsTests.java diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java index 8db05f89fd..7a8c554f53 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java @@ -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 diff --git a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownParentEndpointTests.java b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownParentEndpointTests.java index 215cb1aa27..1a9b8deb18 100644 --- a/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownParentEndpointTests.java +++ b/spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownParentEndpointTests.java @@ -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 diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerDirectMockMvcTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerDirectMockMvcTests.java index 741ec9329b..708683cb47 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerDirectMockMvcTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerDirectMockMvcTests.java @@ -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) { diff --git a/spring-boot/src/main/java/org/springframework/boot/test/ApplicationContextTestUtils.java b/spring-boot/src/main/java/org/springframework/boot/test/ApplicationContextTestUtils.java new file mode 100644 index 0000000000..41f3182aec --- /dev/null +++ b/spring-boot/src/main/java/org/springframework/boot/test/ApplicationContextTestUtils.java @@ -0,0 +1,46 @@ +/* + * 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); + } + } + +} diff --git a/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java index 5404bf17ff..21eb9f0b7c 100644 --- a/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java @@ -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 diff --git a/spring-boot/src/test/java/org/springframework/boot/test/ApplicationContextTestUtilsTests.java b/spring-boot/src/test/java/org/springframework/boot/test/ApplicationContextTestUtilsTests.java new file mode 100644 index 0000000000..d17e07223c --- /dev/null +++ b/spring-boot/src/test/java/org/springframework/boot/test/ApplicationContextTestUtilsTests.java @@ -0,0 +1,57 @@ +/* + * 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(); + } +}