mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-09-24 07:39:09 +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}
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
/*
|
||||
* Copyright 2002-present the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
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;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.test.context.ContextConfiguration;
|
||||
import org.springframework.test.context.junit.jupiter.SpringExtension;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
/**
|
||||
* Integration tests for detection of default context configuration within
|
||||
* nested test class hierarchies without the use of {@link ContextConfiguration}.
|
||||
*
|
||||
* @author Sam Brannen
|
||||
* @since 7.0.2
|
||||
* @see <a href="https://github.com/spring-projects/spring-framework/issues/31456">gh-31456</a>
|
||||
*/
|
||||
@ExtendWith(SpringExtension.class)
|
||||
class DefaultContextConfigurationDetectionWithNestedTests {
|
||||
|
||||
@Autowired
|
||||
String greeting;
|
||||
|
||||
@Test
|
||||
void test(@Autowired String localGreeting) {
|
||||
// This class must NOT be annotated with @SpringJUnitConfig or @ContextConfiguration.
|
||||
assertThat(AnnotatedElementUtils.hasAnnotation(getClass(), ContextConfiguration.class)).isFalse();
|
||||
|
||||
assertThat(greeting).isEqualTo("TEST");
|
||||
assertThat(localGreeting).isEqualTo("TEST");
|
||||
}
|
||||
|
||||
@Nested
|
||||
class NestedTests {
|
||||
|
||||
@Test
|
||||
void test(@Autowired String localGreeting) {
|
||||
assertThat(greeting).isEqualTo("TEST");
|
||||
assertThat(localGreeting).isEqualTo("TEST");
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
static class DefaultConfig {
|
||||
|
||||
@Bean
|
||||
String greeting() {
|
||||
return "TEST";
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user