From a07033f3fbd2e4a918d095ecda554a330424fb8f Mon Sep 17 00:00:00 2001 From: Sam Brannen <104798+sbrannen@users.noreply.github.com> Date: Fri, 13 Mar 2026 15:39:05 +0100 Subject: [PATCH] Resolve all default context configuration within test class hierarchies MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prior to this commit, if a superclass or 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 subclass or @⁠Nested test class would not use any default context configuration for the superclass or enclosing test class. Effectively, a default XML configuration file or static nested @⁠Configuration class for the superclass or 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 --- .../AbstractTestContextBootstrapper.java | 54 +------------------ ...ImplicitDefaultConfigClassesBaseTests.java | 2 +- ...citDefaultConfigClassesInheritedTests.java | 11 +--- ...ConfigurationDetectionWithNestedTests.java | 3 +- 4 files changed, 4 insertions(+), 66 deletions(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java index 06162f25e41..d5a654935db 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java @@ -20,11 +20,9 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; -import java.util.stream.Collectors; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; @@ -80,13 +78,6 @@ import org.springframework.util.StringUtils; */ public abstract class AbstractTestContextBootstrapper implements TestContextBootstrapper { - private static final String IGNORED_DEFAULT_CONFIG_MESSAGE = """ - For test class [%1$s], the following 'default' context configuration %2$s were detected \ - but are currently ignored: %3$s. In Spring Framework 7.1, these %2$s will no longer be ignored. \ - Please update your test configuration accordingly. For details, see: \ - https://docs.spring.io/spring-framework/reference/testing/testcontext-framework/ctx-management/default-config.html"""; - - private final Log logger = LogFactory.getLog(getClass()); private @Nullable BootstrapContext bootstrapContext; @@ -262,12 +253,9 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) { List defaultConfigAttributesList = - Collections.singletonList(new ContextConfigurationAttributes(testClass)); - // for 7.1: ContextLoaderUtils.resolveDefaultContextConfigurationAttributes(testClass); - + ContextLoaderUtils.resolveDefaultContextConfigurationAttributes(testClass); MergedContextConfiguration mergedConfig = buildMergedContextConfiguration( testClass, defaultConfigAttributesList, null, cacheAwareContextLoaderDelegate, false); - logWarningForIgnoredDefaultConfig(mergedConfig, cacheAwareContextLoaderDelegate); if (logger.isTraceEnabled()) { logger.trace(String.format( @@ -283,46 +271,6 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot return mergedConfig; } - /** - * In Spring Framework 7.1, we will use the "complete" list of default config attributes. - * In the interim, we log a warning if the "current" detected config differs from the - * "complete" detected config, which signals that some default configuration is currently - * being ignored. - */ - private void logWarningForIgnoredDefaultConfig(MergedContextConfiguration mergedConfig, - CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) { - - if (logger.isWarnEnabled()) { - Class testClass = mergedConfig.getTestClass(); - List completeDefaultConfigAttributesList = - ContextLoaderUtils.resolveDefaultContextConfigurationAttributes(testClass); - MergedContextConfiguration completeMergedConfig = buildMergedContextConfiguration( - testClass, completeDefaultConfigAttributesList, null, - cacheAwareContextLoaderDelegate, false); - - if (!Arrays.equals(mergedConfig.getClasses(), completeMergedConfig.getClasses())) { - Set> currentClasses = new HashSet<>(Arrays.asList(mergedConfig.getClasses())); - String ignoredClasses = Arrays.stream(completeMergedConfig.getClasses()) - .filter(clazz -> !currentClasses.contains(clazz)) - .map(Class::getName) - .collect(Collectors.joining(", ")); - if (!ignoredClasses.isEmpty()) { - logger.warn(IGNORED_DEFAULT_CONFIG_MESSAGE.formatted(testClass.getName(), "classes", ignoredClasses)); - } - } - - if (!Arrays.equals(mergedConfig.getLocations(), completeMergedConfig.getLocations())) { - Set currentLocations = new HashSet<>(Arrays.asList(mergedConfig.getLocations())); - String ignoredLocations = Arrays.stream(completeMergedConfig.getLocations()) - .filter(location -> !currentLocations.contains(location)) - .collect(Collectors.joining(", ")); - if (!ignoredLocations.isEmpty()) { - logger.warn(IGNORED_DEFAULT_CONFIG_MESSAGE.formatted(testClass.getName(), "locations", ignoredLocations)); - } - } - } - } - /** * Build the {@linkplain MergedContextConfiguration merged context configuration} * for the supplied {@link Class testClass}, context configuration attributes, diff --git a/spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesBaseTests.java b/spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesBaseTests.java index 36c2718f47a..1828e0df7c9 100644 --- a/spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesBaseTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesBaseTests.java @@ -46,7 +46,7 @@ class ImplicitDefaultConfigClassesBaseTests { @Test - void greeting1AndPuzzle1() { + final void greeting1AndPuzzle1() { // This class must NOT be annotated with @SpringJUnitConfig or @ContextConfiguration. assertThat(AnnotatedElementUtils.hasAnnotation(getClass(), ContextConfiguration.class)).isFalse(); diff --git a/spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesInheritedTests.java b/spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesInheritedTests.java index 93699c072ce..7a33a39c882 100644 --- a/spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesInheritedTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesInheritedTests.java @@ -41,14 +41,6 @@ class ImplicitDefaultConfigClassesInheritedTests extends ImplicitDefaultConfigCl String greeting2; - // To be removed in favor of base class method in 7.1 - @Test - @Override - void greeting1AndPuzzle1() { - assertThat(greeting1).isEqualTo("TEST 2"); - assertThat(puzzle1).isEqualTo(222); - } - @Test void greeting2() { // This class must NOT be annotated with @SpringJUnitConfig or @ContextConfiguration. @@ -59,8 +51,7 @@ class ImplicitDefaultConfigClassesInheritedTests extends ImplicitDefaultConfigCl @Test void greetings(@Autowired List greetings) { - assertThat(greetings).containsExactly("TEST 2"); - // for 7.1: assertThat(greetings).containsExactly("TEST 1", "TEST 2"); + assertThat(greetings).containsExactly("TEST 1", "TEST 2"); } diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DefaultContextConfigurationDetectionWithNestedTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DefaultContextConfigurationDetectionWithNestedTests.java index 615c2355837..8e1d361f342 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DefaultContextConfigurationDetectionWithNestedTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DefaultContextConfigurationDetectionWithNestedTests.java @@ -16,6 +16,7 @@ package org.springframework.test.context.junit.jupiter.nested; +import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -53,7 +54,6 @@ class DefaultContextConfigurationDetectionWithNestedTests { } - /** for 7.1: @Nested class NestedTests { @@ -63,7 +63,6 @@ class DefaultContextConfigurationDetectionWithNestedTests { assertThat(localGreeting).isEqualTo("TEST"); } } - */ @Configuration