mirror of
https://github.com/spring-projects/spring-framework
synced 2026-06-08 17:33:33 +00:00
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
This commit is contained in:
+1
-1
@@ -250,7 +250,7 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
|
||||
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) {
|
||||
|
||||
List<ContextConfigurationAttributes> defaultConfigAttributesList =
|
||||
Collections.singletonList(new ContextConfigurationAttributes(testClass));
|
||||
ContextLoaderUtils.resolveDefaultContextConfigurationAttributes(testClass);
|
||||
|
||||
ContextLoader contextLoader = resolveContextLoader(testClass, defaultConfigAttributesList);
|
||||
if (logger.isTraceEnabled()) {
|
||||
|
||||
+40
@@ -30,6 +30,7 @@ import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.ContextConfigurationAttributes;
|
||||
import org.springframework.test.context.ContextHierarchy;
|
||||
import org.springframework.test.context.SmartContextLoader;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils.AnnotationDescriptor;
|
||||
import org.springframework.test.context.TestContextAnnotationUtils.UntypedAnnotationDescriptor;
|
||||
import org.springframework.util.Assert;
|
||||
@@ -92,6 +93,7 @@ abstract class ContextLoaderUtils {
|
||||
* {@code @ContextHierarchy} as top-level annotations.
|
||||
* @since 3.2.2
|
||||
* @see #buildContextHierarchyMap(Class)
|
||||
* @see #resolveDefaultContextConfigurationAttributes(Class)
|
||||
* @see #resolveContextConfigurationAttributes(Class)
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
@@ -215,6 +217,44 @@ abstract class ContextLoaderUtils {
|
||||
return map;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the list of <em>default</em> {@linkplain ContextConfigurationAttributes
|
||||
* context configuration attributes} for the supplied {@linkplain Class test class}
|
||||
* and its superclasses and enclosing classes.
|
||||
* <p>Use this method instead of {@link #resolveContextConfigurationAttributes(Class)}
|
||||
* if neither {@link ContextConfiguration @ContextConfiguration} nor
|
||||
* {@link ContextHierarchy @ContextHierarchy} is present in the class hierarchy
|
||||
* of the supplied test class.
|
||||
* @param testClass the class for which to resolve the configuration attributes
|
||||
* (must not be {@code null})
|
||||
* @return the list of configuration attributes for the specified class, ordered
|
||||
* <em>bottom-up</em> (i.e., as if we were traversing up the class hierarchy
|
||||
* and enclosing class hierarchy); never {@code null}
|
||||
* @throws IllegalArgumentException if the supplied class is {@code null}
|
||||
* @since 7.0.2
|
||||
*/
|
||||
static List<ContextConfigurationAttributes> resolveDefaultContextConfigurationAttributes(Class<?> testClass) {
|
||||
Assert.notNull(testClass, "Class must not be null");
|
||||
List<ContextConfigurationAttributes> results = new ArrayList<>();
|
||||
resolveDefaultContextConfigurationAttributes(results, testClass);
|
||||
return results;
|
||||
}
|
||||
|
||||
private static void resolveDefaultContextConfigurationAttributes(
|
||||
List<ContextConfigurationAttributes> results, Class<?> testClass) {
|
||||
|
||||
results.add(0, new ContextConfigurationAttributes(testClass));
|
||||
|
||||
Class<?> superclass = testClass.getSuperclass();
|
||||
if (superclass != null && superclass != Object.class) {
|
||||
resolveDefaultContextConfigurationAttributes(results, superclass);
|
||||
}
|
||||
|
||||
if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) {
|
||||
resolveDefaultContextConfigurationAttributes(results, testClass.getEnclosingClass());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve the list of {@linkplain ContextConfigurationAttributes context
|
||||
* configuration attributes} for the supplied {@linkplain Class test class}
|
||||
|
||||
Reference in New Issue
Block a user