Skip to content

Commit 8b90410

Browse files
committed
Update ...DirtiesContextTests to get beans early
Change ContextHierarchyDirtiesContextTests to obtain beans whilst the ApplicationContext is still open, rather than trying to obtain them after the context has been closed.
1 parent 3126a4c commit 8b90410

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

spring-test/src/test/java/org/springframework/test/context/ContextHierarchyDirtiesContextTests.java

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,19 @@ public class ContextHierarchyDirtiesContextTests {
4949

5050
private static ApplicationContext context;
5151

52+
private static String foo;
53+
54+
private static String bar;
55+
56+
private static String baz;
57+
5258

5359
@After
5460
public void cleanUp() {
5561
ContextHierarchyDirtiesContextTests.context = null;
62+
ContextHierarchyDirtiesContextTests.foo = null;
63+
ContextHierarchyDirtiesContextTests.bar = null;
64+
ContextHierarchyDirtiesContextTests.baz = null;
5665
}
5766

5867
@Test
@@ -85,17 +94,17 @@ private void runTestAndVerifyHierarchies(Class<? extends FooTestCase> testClass,
8594
assertThat(ContextHierarchyDirtiesContextTests.context, notNullValue());
8695

8796
ConfigurableApplicationContext bazContext = (ConfigurableApplicationContext) ContextHierarchyDirtiesContextTests.context;
88-
assertEquals("baz", bazContext.getBean("bean", String.class));
97+
assertEquals("baz", ContextHierarchyDirtiesContextTests.baz);
8998
assertThat("bazContext#isActive()", bazContext.isActive(), is(isBazContextActive));
9099

91100
ConfigurableApplicationContext barContext = (ConfigurableApplicationContext) bazContext.getParent();
92101
assertThat(barContext, notNullValue());
93-
assertEquals("bar", barContext.getBean("bean", String.class));
102+
assertEquals("bar", ContextHierarchyDirtiesContextTests.bar);
94103
assertThat("barContext#isActive()", barContext.isActive(), is(isBarContextActive));
95104

96105
ConfigurableApplicationContext fooContext = (ConfigurableApplicationContext) barContext.getParent();
97106
assertThat(fooContext, notNullValue());
98-
assertEquals("foo", fooContext.getBean("bean", String.class));
107+
assertEquals("foo", ContextHierarchyDirtiesContextTests.foo);
99108
assertThat("fooContext#isActive()", fooContext.isActive(), is(isFooContextActive));
100109
}
101110

@@ -115,9 +124,11 @@ public String bean() {
115124
}
116125
}
117126

118-
119127
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
120128
ContextHierarchyDirtiesContextTests.context = applicationContext;
129+
ContextHierarchyDirtiesContextTests.baz = applicationContext.getBean("bean", String.class);
130+
ContextHierarchyDirtiesContextTests.bar = applicationContext.getParent().getBean("bean", String.class);
131+
ContextHierarchyDirtiesContextTests.foo = applicationContext.getParent().getParent().getBean("bean", String.class);
121132
}
122133
}
123134

0 commit comments

Comments
 (0)