Skip to content

Commit fbb3c5c

Browse files
committed
Switch the default @NestedTestConfiguration mode to INHERIT.
See gh-19930
1 parent 8c47c7c commit fbb3c5c

12 files changed

+39
-32
lines changed

spring-test/src/main/java/org/springframework/test/context/NestedTestConfiguration.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@
3030
*
3131
* <p>If {@code @NestedTestConfiguration} is not <em>present</em> or
3232
* <em>meta-present</em> on a test class, configuration from the test class will
33-
* not propagate to inner test classes (see {@link EnclosingConfiguration#OVERRIDE}).
34-
* Consequently, inner test classes will have to declare their own Spring test
35-
* configuration annotations. If you wish for an inner test class to inherit
36-
* configuration from its enclosing class, annotate either the inner test class
37-
* or the enclosing class with
38-
* {@code @NestedTestConfiguration(EnclosingConfiguration.INHERIT)}. Note that
39-
* a {@code @NestedTestConfiguration(...)} declaration is inherited within the
33+
* propagate to inner test classes (see {@link EnclosingConfiguration#INHERIT}).
34+
* If {@code @NestedTestConfiguration(OVERRIDE)} is used to switch the mode,
35+
* inner test classes will have to declare their own Spring test configuration
36+
* annotations. If you wish to explicitly configure the mode, annotate either
37+
* the inner test class or the enclosing class with
38+
* {@code @NestedTestConfiguration(...}. Note that a
39+
* {@code @NestedTestConfiguration(...)} declaration is inherited within the
4040
* superclass hierarchy as well as within the enclosing class hierarchy. Thus,
4141
* there is no need to redeclare the annotation unless you wish to switch the
4242
* mode.

spring-test/src/main/java/org/springframework/test/util/MetaAnnotationUtils.java

+14-18
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@
6868
*/
6969
public abstract class MetaAnnotationUtils {
7070

71-
private static final ConcurrentLruCache<Class<?>, SearchStrategy> cachedSearchStrategies =
72-
new ConcurrentLruCache<>(32, MetaAnnotationUtils::lookUpSearchStrategy);
71+
private static final ConcurrentLruCache<Class<?>, EnclosingConfiguration> cachedSearchStrategies =
72+
new ConcurrentLruCache<>(32, MetaAnnotationUtils::lookUpEnclosingConfiguration);
7373

7474

7575
/**
@@ -301,35 +301,29 @@ private static UntypedAnnotationDescriptor findAnnotationDescriptorForTypes(@Nul
301301
* class should be searched
302302
* @since 5.3
303303
* @see ClassUtils#isInnerClass(Class)
304-
* @see #getSearchStrategy(Class)
305304
*/
306305
public static boolean searchEnclosingClass(Class<?> clazz) {
307306
return (ClassUtils.isInnerClass(clazz) &&
308-
getSearchStrategy(clazz) == SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES);
307+
getEnclosingConfiguration(clazz) == EnclosingConfiguration.INHERIT);
309308
}
310309

311310
/**
312-
* Get the {@link SearchStrategy} for the supplied class.
311+
* Get the {@link EnclosingConfiguration} mode for the supplied class.
313312
* @param clazz the class for which the search strategy should be resolved
314313
* @return the resolved search strategy
315314
* @since 5.3
316315
*/
317-
private static SearchStrategy getSearchStrategy(Class<?> clazz) {
316+
private static EnclosingConfiguration getEnclosingConfiguration(Class<?> clazz) {
318317
return cachedSearchStrategies.get(clazz);
319318
}
320319

321-
private static SearchStrategy lookUpSearchStrategy(Class<?> clazz) {
322-
EnclosingConfiguration enclosingConfiguration =
323-
MergedAnnotations.from(clazz, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES)
320+
private static EnclosingConfiguration lookUpEnclosingConfiguration(Class<?> clazz) {
321+
// TODO Make the default EnclosingConfiguration mode globally configurable via SpringProperties.
322+
return MergedAnnotations.from(clazz, SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES)
324323
.stream(NestedTestConfiguration.class)
325324
.map(mergedAnnotation -> mergedAnnotation.getEnum("value", EnclosingConfiguration.class))
326325
.findFirst()
327-
.orElse(EnclosingConfiguration.OVERRIDE);
328-
// TODO Switch the default EnclosingConfiguration mode to INHERIT.
329-
// TODO Make the default EnclosingConfiguration mode globally configurable via SpringProperties.
330-
return (enclosingConfiguration == EnclosingConfiguration.INHERIT ?
331-
SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES :
332-
SearchStrategy.TYPE_HIERARCHY);
326+
.orElse(EnclosingConfiguration.INHERIT);
333327
}
334328

335329
private static void assertNonEmptyAnnotationTypeArray(Class<?>[] annotationTypes, String message) {
@@ -505,10 +499,12 @@ public AnnotationDescriptor<T> next() {
505499
*/
506500
@SuppressWarnings("unchecked")
507501
public Set<T> findAllLocalMergedAnnotations() {
508-
Class<T> annotationType = (Class<T>) getAnnotationType();
509-
SearchStrategy searchStrategy = getSearchStrategy(getRootDeclaringClass());
502+
SearchStrategy searchStrategy =
503+
(getEnclosingConfiguration(getRootDeclaringClass()) == EnclosingConfiguration.INHERIT ?
504+
SearchStrategy.TYPE_HIERARCHY_AND_ENCLOSING_CLASSES :
505+
SearchStrategy.TYPE_HIERARCHY);
510506
return MergedAnnotations.from(getRootDeclaringClass(), searchStrategy, RepeatableContainers.none())
511-
.stream(annotationType)
507+
.stream((Class<T>) getAnnotationType())
512508
.filter(MergedAnnotationPredicates.firstRunOf(MergedAnnotation::getAggregateIndex))
513509
.collect(MergedAnnotationCollectors.toAnnotationSet());
514510
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*/
4747
@SpringJUnitConfig(Config1.class)
4848
@ActiveProfiles("1")
49+
@NestedTestConfiguration(OVERRIDE) // since INHERIT is now the global default
4950
class ActiveProfilesNestedTests {
5051

5152
@Autowired

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,12 +25,14 @@
2525
import org.springframework.beans.factory.annotation.Value;
2626
import org.springframework.context.annotation.Bean;
2727
import org.springframework.context.annotation.Configuration;
28+
import org.springframework.test.context.NestedTestConfiguration;
2829
import org.springframework.test.context.junit.SpringJUnitJupiterTestSuite;
2930
import org.springframework.test.context.junit.jupiter.SpringExtension;
3031
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
3132
import org.springframework.test.context.junit.jupiter.nested.ConstructorInjectionNestedTests.TopLevelConfig;
3233

3334
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.springframework.test.context.NestedTestConfiguration.EnclosingConfiguration.OVERRIDE;
3436

3537
/**
3638
* Integration tests that verify support for {@code @Nested} test classes in conjunction
@@ -47,6 +49,7 @@
4749
* @see org.springframework.test.context.junit4.nested.NestedTestsWithSpringRulesTests
4850
*/
4951
@SpringJUnitConfig(TopLevelConfig.class)
52+
@NestedTestConfiguration(OVERRIDE) // since INHERIT is now the global default
5053
class ConstructorInjectionNestedTests {
5154

5255
final String foo;

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

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @see org.springframework.test.context.junit4.nested.NestedTestsWithSpringRulesTests
4545
*/
4646
@SpringJUnitConfig(TopLevelConfig.class)
47+
@NestedTestConfiguration(OVERRIDE) // since INHERIT is now the global default
4748
class ContextConfigurationNestedTests {
4849

4950
private static final String FOO = "foo";

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

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
*/
4646
@ExtendWith(SpringExtension.class)
4747
@ContextHierarchy(@ContextConfiguration(classes = ParentConfig.class))
48+
@NestedTestConfiguration(OVERRIDE) // since INHERIT is now the global default
4849
class ContextHierarchyNestedTests {
4950

5051
private static final String FOO = "foo";

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

-6
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,6 @@ private int countRowsInTable(String tableName) {
6767
}
6868

6969
@Nested
70-
// NOTE: the following @SpringJUnitConfig declaration must NOT be removed.
71-
// This was added before the TestContext framework looked up configuration
72-
// on enclosing classes for @Nested test classes. As such, this serves as a
73-
// regression test and cannot be changed.
74-
@SpringJUnitConfig(PopulatedSchemaDatabaseConfig.class)
75-
@Transactional
7670
class NestedTests {
7771

7872
@Autowired

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

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
*/
4444
@SpringJUnitConfig
4545
@TestConstructor(autowireMode = ALL)
46+
@NestedTestConfiguration(OVERRIDE) // since INHERIT is now the global default
4647
class TestConstructorNestedTests {
4748

4849
TestConstructorNestedTests(String text) {

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

+1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
*/
4747
@SpringJUnitConfig
4848
@TestExecutionListeners(FooTestExecutionListener.class)
49+
@NestedTestConfiguration(OVERRIDE) // since INHERIT is now the global default
4950
class TestExecutionListenersNestedTests {
5051

5152
private static final String FOO = "foo";

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

+7
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
*/
4343
@SpringJUnitConfig(Config.class)
4444
@TestPropertySource(properties = "p1 = v1")
45+
@NestedTestConfiguration(OVERRIDE) // since INHERIT is now the global default
4546
class TestPropertySourceNestedTests {
4647

4748
@Autowired
@@ -160,6 +161,12 @@ void propertiesInEnvironment() {
160161
}
161162

162163
@Nested
164+
// The following explicit INHERIT is necessary since this nested
165+
// test class implements an interface whose enclosing class is
166+
// annotated with @NestedTestConfiguration(OVERRIDE). In other
167+
// words, the local declaration overrides the declaration
168+
// "inherited" via the interface.
169+
@NestedTestConfiguration(INHERIT)
163170
class L5WithInheritedConfigAndTestInterfaceTests implements TestInterface {
164171

165172
@Autowired

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

+1
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
@SpringJUnitConfig
5454
@Transactional
5555
@Commit
56+
@NestedTestConfiguration(OVERRIDE) // since INHERIT is now the global default
5657
class TransactionalNestedTests {
5758

5859
@Test

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

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @see org.springframework.test.context.junit4.nested.NestedTestsWithSpringRulesTests
4545
*/
4646
@SpringJUnitWebConfig(Config.class)
47+
@NestedTestConfiguration(OVERRIDE) // since INHERIT is now the global default
4748
class WebAppConfigurationNestedTests {
4849

4950
@Test

0 commit comments

Comments
 (0)