Browse Source

Resolve all default context configuration within test class hierarchies

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
pull/36467/head
Sam Brannen 5 days ago
parent
commit
a07033f3fb
  1. 54
      spring-test/src/main/java/org/springframework/test/context/support/AbstractTestContextBootstrapper.java
  2. 2
      spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesBaseTests.java
  3. 11
      spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesInheritedTests.java
  4. 3
      spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DefaultContextConfigurationDetectionWithNestedTests.java

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

@ -20,11 +20,9 @@ import java.util.ArrayList; @@ -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; @@ -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 @@ -262,12 +253,9 @@ public abstract class AbstractTestContextBootstrapper implements TestContextBoot
CacheAwareContextLoaderDelegate cacheAwareContextLoaderDelegate) {
List<ContextConfigurationAttributes> 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 @@ -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<ContextConfigurationAttributes> completeDefaultConfigAttributesList =
ContextLoaderUtils.resolveDefaultContextConfigurationAttributes(testClass);
MergedContextConfiguration completeMergedConfig = buildMergedContextConfiguration(
testClass, completeDefaultConfigAttributesList, null,
cacheAwareContextLoaderDelegate, false);
if (!Arrays.equals(mergedConfig.getClasses(), completeMergedConfig.getClasses())) {
Set<Class<?>> 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<String> 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,

2
spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesBaseTests.java

@ -46,7 +46,7 @@ class ImplicitDefaultConfigClassesBaseTests { @@ -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();

11
spring-test/src/test/java/org/springframework/test/context/config/ImplicitDefaultConfigClassesInheritedTests.java

@ -41,14 +41,6 @@ class ImplicitDefaultConfigClassesInheritedTests extends ImplicitDefaultConfigCl @@ -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 @@ -59,8 +51,7 @@ class ImplicitDefaultConfigClassesInheritedTests extends ImplicitDefaultConfigCl
@Test
void greetings(@Autowired List<String> greetings) {
assertThat(greetings).containsExactly("TEST 2");
// for 7.1: assertThat(greetings).containsExactly("TEST 1", "TEST 2");
assertThat(greetings).containsExactly("TEST 1", "TEST 2");
}

3
spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DefaultContextConfigurationDetectionWithNestedTests.java

@ -16,6 +16,7 @@ @@ -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 { @@ -53,7 +54,6 @@ class DefaultContextConfigurationDetectionWithNestedTests {
}
/** for 7.1:
@Nested
class NestedTests {
@ -63,7 +63,6 @@ class DefaultContextConfigurationDetectionWithNestedTests { @@ -63,7 +63,6 @@ class DefaultContextConfigurationDetectionWithNestedTests {
assertThat(localGreeting).isEqualTo("TEST");
}
}
*/
@Configuration

Loading…
Cancel
Save