Skip to content

Commit b646231

Browse files
snicollDave Syer
authored and
Dave Syer
committed
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
1 parent f83395b commit b646231

File tree

6 files changed

+112
-17
lines changed

6 files changed

+112
-17
lines changed

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/autoconfigure/SpringApplicationHierarchyTests.java

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import org.junit.Test;
2121
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2222
import org.springframework.boot.builder.SpringApplicationBuilder;
23-
import org.springframework.context.ApplicationContext;
23+
import org.springframework.boot.test.ApplicationContextTestUtils;
2424
import org.springframework.context.ConfigurableApplicationContext;
2525

2626
/**
@@ -34,13 +34,7 @@ public class SpringApplicationHierarchyTests {
3434

3535
@After
3636
public void after() {
37-
if (this.context != null) {
38-
ApplicationContext parentContext = this.context.getParent();
39-
if (parentContext instanceof ConfigurableApplicationContext) {
40-
((ConfigurableApplicationContext) parentContext).close();
41-
}
42-
this.context.close();
43-
}
37+
ApplicationContextTestUtils.closeAll(this.context);
4438
}
4539

4640
@Test

spring-boot-actuator/src/test/java/org/springframework/boot/actuate/endpoint/ShutdownParentEndpointTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.junit.Test;
2424
import org.springframework.boot.builder.SpringApplicationBuilder;
2525
import org.springframework.boot.context.properties.EnableConfigurationProperties;
26+
import org.springframework.boot.test.ApplicationContextTestUtils;
2627
import org.springframework.context.ApplicationListener;
2728
import org.springframework.context.ConfigurableApplicationContext;
2829
import org.springframework.context.annotation.Bean;
@@ -44,9 +45,7 @@ public class ShutdownParentEndpointTests {
4445

4546
@After
4647
public void close() {
47-
if (this.context != null) {
48-
this.context.close();
49-
}
48+
ApplicationContextTestUtils.closeAll(this.context);
5049
}
5150

5251
@Test

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/BasicErrorControllerDirectMockMvcTests.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.springframework.boot.SpringApplication;
2828
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
2929
import org.springframework.boot.builder.SpringApplicationBuilder;
30+
import org.springframework.boot.test.ApplicationContextTestUtils;
3031
import org.springframework.context.annotation.Configuration;
3132
import org.springframework.context.annotation.Import;
3233
import org.springframework.http.MediaType;
@@ -55,9 +56,7 @@ public class BasicErrorControllerDirectMockMvcTests {
5556

5657
@After
5758
public void close() {
58-
if (this.wac != null) {
59-
this.wac.close();
60-
}
59+
ApplicationContextTestUtils.closeAll(this.wac);
6160
}
6261

6362
public void setup(ConfigurableWebApplicationContext context) {
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test;
18+
19+
20+
import org.springframework.context.ApplicationContext;
21+
import org.springframework.context.ConfigurableApplicationContext;
22+
23+
/**
24+
* Application context related test utilities.
25+
*
26+
* @author Stephane Nicoll
27+
* @since 1.1.1
28+
*/
29+
public abstract class ApplicationContextTestUtils {
30+
31+
/**
32+
* Closes this {@link ApplicationContext} and its parent hierarchy
33+
* if any.
34+
* @param context the context to close (can be {@code null})
35+
*/
36+
public static void closeAll(ApplicationContext context) {
37+
if (context != null) {
38+
ApplicationContext parent = context.getParent();
39+
if (context instanceof ConfigurableApplicationContext) {
40+
((ConfigurableApplicationContext) context).close();
41+
}
42+
closeAll(parent);
43+
}
44+
}
45+
46+
}

spring-boot/src/test/java/org/springframework/boot/builder/SpringApplicationBuilderTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
import org.junit.After;
2424
import org.junit.Test;
25+
26+
import org.springframework.boot.test.ApplicationContextTestUtils;
2527
import org.springframework.context.ApplicationContext;
2628
import org.springframework.context.ApplicationContextInitializer;
2729
import org.springframework.context.ConfigurableApplicationContext;
@@ -52,9 +54,7 @@ public class SpringApplicationBuilderTests {
5254

5355
@After
5456
public void close() {
55-
if (this.context != null) {
56-
this.context.close();
57-
}
57+
ApplicationContextTestUtils.closeAll(this.context);
5858
}
5959

6060
@Test
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Copyright 2012-2014 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.test;
18+
19+
import static org.mockito.BDDMockito.given;
20+
import static org.mockito.Mockito.*;
21+
22+
import org.junit.Test;
23+
24+
import org.springframework.context.ApplicationContext;
25+
import org.springframework.context.ConfigurableApplicationContext;
26+
27+
/**
28+
*
29+
* @author Stephane Nicoll
30+
*/
31+
public class ApplicationContextTestUtilsTests {
32+
33+
@Test
34+
public void closeNull() {
35+
ApplicationContextTestUtils.closeAll(null);
36+
}
37+
38+
@Test
39+
public void closeNonClosableContext() {
40+
ApplicationContext mock = mock(ApplicationContext.class);
41+
ApplicationContextTestUtils.closeAll(mock);
42+
}
43+
44+
@Test
45+
public void closeContextAndParent() {
46+
ConfigurableApplicationContext mock = mock(ConfigurableApplicationContext.class);
47+
ConfigurableApplicationContext parent = mock(ConfigurableApplicationContext.class);
48+
given(mock.getParent()).willReturn(parent);
49+
given(parent.getParent()).willReturn(null);
50+
51+
ApplicationContextTestUtils.closeAll(mock);
52+
verify(mock).getParent();
53+
verify(mock).close();
54+
verify(parent).getParent();
55+
verify(parent).close();
56+
}
57+
}

0 commit comments

Comments
 (0)