Skip to content

Commit 4ae471d

Browse files
committed
Resolve all default context configuration within @⁠Nested hierarchy
Prior to this commit, if an enclosing test class (such as one annotated with @⁠SpringBootTest or simply @⁠ExtendWith(SpringExtension.class)) was not annotated with @⁠ContextConfiguration (or @⁠Import with @⁠SpringBootTest), the ApplicationContext loaded for a @⁠Nested test class would not use any default context configuration for the enclosing test class. Effectively, a default XML configuration file or static nested @⁠Configuration class for the enclosing test class was not discovered by the AbstractTestContextBootstrapper when attempting to build the MergedContextConfiguration (application context cache key). To address that, this commit introduces a new resolveDefaultContextConfigurationAttributes() method in ContextLoaderUtils which is responsible for creating instances of ContextConfigurationAttributes for all superclasses and enclosing classes. This effectively enables AbstractTestContextBootstrapper to delegate to the resolved SmartContextLoader to properly detect a default XML configuration file or static nested @⁠Configuration class even if such classes are not annotated with @⁠ContextConfiguration. Closes gh-31456
1 parent 75e3f44 commit 4ae471d

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed

spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ private MergedContextConfiguration buildDefaultMergedContextConfiguration(Class<
250250
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) {
251251

252252
List<ContextConfigurationAttributes> defaultConfigAttributesList =
253-
Collections.singletonList(new ContextConfigurationAttributes(testClass));
253+
ContextLoaderUtils.resolveDefaultContextConfigurationAttributes(testClass);
254254

255255
ContextLoader contextLoader = resolveContextLoader(testClass, defaultConfigAttributesList);
256256
if (logger.isTraceEnabled()) {

spring-test/src/main/java/org/springframework/test/context/support/ContextLoaderUtils.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.test.context.ContextConfigurationAttributes;
3131
import org.springframework.test.context.ContextHierarchy;
3232
import org.springframework.test.context.SmartContextLoader;
33+
import org.springframework.test.context.TestContextAnnotationUtils;
3334
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
3435
import org.springframework.test.context.TestContextAnnotationUtils.UntypedAnnotationDescriptor;
3536
import org.springframework.util.Assert;
@@ -92,6 +93,7 @@ abstract class ContextLoaderUtils {
9293
* {@code @ContextHierarchy} as top-level annotations.
9394
* @since 3.2.2
9495
* @see #buildContextHierarchyMap(Class)
96+
* @see #resolveDefaultContextConfigurationAttributes(Class)
9597
* @see #resolveContextConfigurationAttributes(Class)
9698
*/
9799
@SuppressWarnings("unchecked")
@@ -215,6 +217,44 @@ static Map<String, List<ContextConfigurationAttributes>> buildContextHierarchyMa
215217
return map;
216218
}
217219

220+
/**
221+
* Resolve the list of <em>default</em> {@linkplain ContextConfigurationAttributes
222+
* context configuration attributes} for the supplied {@linkplain Class test class}
223+
* and its superclasses and enclosing classes.
224+
* <p>Use this method instead of {@link #resolveContextConfigurationAttributes(Class)}
225+
* if neither {@link ContextConfiguration @ContextConfiguration} nor
226+
* {@link ContextHierarchy @ContextHierarchy} is present in the class hierarchy
227+
* of the supplied test class.
228+
* @param testClass the class for which to resolve the configuration attributes
229+
* (must not be {@code null})
230+
* @return the list of configuration attributes for the specified class, ordered
231+
* <em>bottom-up</em> (i.e., as if we were traversing up the class hierarchy
232+
* and enclosing class hierarchy); never {@code null}
233+
* @throws IllegalArgumentException if the supplied class is {@code null}
234+
* @since 7.0.2
235+
*/
236+
static List<ContextConfigurationAttributes> resolveDefaultContextConfigurationAttributes(Class<?> testClass) {
237+
Assert.notNull(testClass, "Class must not be null");
238+
List<ContextConfigurationAttributes> results = new ArrayList<>();
239+
resolveDefaultContextConfigurationAttributes(results, testClass);
240+
return results;
241+
}
242+
243+
private static void resolveDefaultContextConfigurationAttributes(
244+
List<ContextConfigurationAttributes> results, Class<?> testClass) {
245+
246+
results.add(0, new ContextConfigurationAttributes(testClass));
247+
248+
Class<?> superclass = testClass.getSuperclass();
249+
if (superclass != null && superclass != Object.class) {
250+
resolveDefaultContextConfigurationAttributes(results, superclass);
251+
}
252+
253+
if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) {
254+
resolveDefaultContextConfigurationAttributes(results, testClass.getEnclosingClass());
255+
}
256+
}
257+
218258
/**
219259
* Resolve the list of {@linkplain ContextConfigurationAttributes context
220260
* configuration attributes} for the supplied {@linkplain Class test class}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* Copyright 2002-present 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+
* https://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.test.context.junit.jupiter.nested;
18+
19+
import org.junit.jupiter.api.Nested;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
22+
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.context.annotation.Bean;
25+
import org.springframework.context.annotation.Configuration;
26+
import org.springframework.core.annotation.AnnotatedElementUtils;
27+
import org.springframework.test.context.ContextConfiguration;
28+
import org.springframework.test.context.junit.jupiter.SpringExtension;
29+
30+
import static org.assertj.core.api.Assertions.assertThat;
31+
32+
/**
33+
* Integration tests for detection of default context configuration within
34+
* nested test class hierarchies without the use of {@link ContextConfiguration}.
35+
*
36+
* @author Sam Brannen
37+
* @since 7.0.2
38+
* @see <a href="https://github.com/spring-projects/spring-framework/issues/31456">gh-31456</a>
39+
*/
40+
@ExtendWith(SpringExtension.class)
41+
class DefaultContextConfigurationDetectionWithNestedTests {
42+
43+
@Autowired
44+
String greeting;
45+
46+
@Test
47+
void test(@Autowired String localGreeting) {
48+
// This class must NOT be annotated with @SpringJUnitConfig or @ContextConfiguration.
49+
assertThat(AnnotatedElementUtils.hasAnnotation(getClass(), ContextConfiguration.class)).isFalse();
50+
51+
assertThat(greeting).isEqualTo("TEST");
52+
assertThat(localGreeting).isEqualTo("TEST");
53+
}
54+
55+
@Nested
56+
class NestedTests {
57+
58+
@Test
59+
void test(@Autowired String localGreeting) {
60+
assertThat(greeting).isEqualTo("TEST");
61+
assertThat(localGreeting).isEqualTo("TEST");
62+
}
63+
}
64+
65+
@Configuration
66+
static class DefaultConfig {
67+
68+
@Bean
69+
String greeting() {
70+
return "TEST";
71+
}
72+
}
73+
74+
}

0 commit comments

Comments
 (0)