Make default @NestedTestConfiguration mode configurable

Prior to this commit, the EnclosingConfiguration mode used in
conjunction with @NestedTestConfiguration defaulted to INHERIT.

In other to allow development teams to change the default to OVERRIDE
(e.g., for compatibility with Spring Framework 5.0 through 5.2), this
commit introduces support for changing the default EnclosingConfiguration
mode globally via a JVM system property or via the SpringProperties
mechanism.

For example, the default may be changed to
EnclosingConfiguration.OVERRIDE by supplying the following JVM system
property via the command line.

-Dspring.test.enclosing.configuration=override

Closes gh-19930
This commit is contained in:
Sam Brannen
2020-10-13 00:09:49 +02:00
parent 7f365942a5
commit 0af09e076b
3 changed files with 122 additions and 5 deletions

View File

@@ -22,22 +22,27 @@ import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.core.SpringProperties;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.NestedTestConfiguration;
import org.springframework.test.util.MetaAnnotationUtils.AnnotationDescriptor;
import org.springframework.test.util.MetaAnnotationUtils.UntypedAnnotationDescriptor;
import org.springframework.transaction.annotation.Transactional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.springframework.test.context.NestedTestConfiguration.EnclosingConfiguration.OVERRIDE;
import static org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptor;
import static org.springframework.test.util.MetaAnnotationUtils.findAnnotationDescriptorForTypes;
import static org.springframework.test.util.MetaAnnotationUtils.searchEnclosingClass;
/**
* Unit tests for {@link MetaAnnotationUtils}.
@@ -48,6 +53,35 @@ import static org.springframework.test.util.MetaAnnotationUtils.findAnnotationDe
*/
class MetaAnnotationUtilsTests {
@Nested
@DisplayName("searchEnclosingClass() tests")
class SearchEnclosingClassTests {
@AfterEach
void clearGlobalFlag() {
setGlobalFlag(null);
}
@Test
void standardDefaultMode() {
assertThat(searchEnclosingClass(OuterTestCase1.class)).isFalse();
assertThat(searchEnclosingClass(OuterTestCase1.NestedTestCase.class)).isTrue();
assertThat(searchEnclosingClass(OuterTestCase1.NestedTestCase.DoubleNestedTestCase.class)).isTrue();
}
@Test
void overriddenDefaultMode() {
setGlobalFlag("\t" + OVERRIDE.name().toLowerCase() + " ");
assertThat(searchEnclosingClass(OuterTestCase2.class)).isFalse();
assertThat(searchEnclosingClass(OuterTestCase2.NestedTestCase.class)).isFalse();
assertThat(searchEnclosingClass(OuterTestCase2.NestedTestCase.DoubleNestedTestCase.class)).isFalse();
}
private void setGlobalFlag(String flag) {
SpringProperties.setProperty(NestedTestConfiguration.ENCLOSING_CONFIGURATION_PROPERTY_NAME, flag);
}
}
@Nested
@DisplayName("findAnnotationDescriptor() tests")
class FindAnnotationDescriptorTests {
@@ -636,4 +670,20 @@ class MetaAnnotationUtilsTests {
static class MetaAnnotatedAndSuperAnnotatedContextConfigClass extends AnnotatedContextConfigClass {
}
// We need two variants of "OuterTestCase", since the results for searchEnclosingClass() get cached.
static class OuterTestCase1 {
class NestedTestCase {
class DoubleNestedTestCase {
}
}
}
// We need two variants of "OuterTestCase", since the results for searchEnclosingClass() get cached.
static class OuterTestCase2 {
class NestedTestCase {
class DoubleNestedTestCase {
}
}
}
}