From 9894174960aa7fc79087b7e271c794101c6735de Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Mon, 21 Aug 2023 17:37:57 +0800 Subject: [PATCH] Allow overriding dynamic property from enclosing class in nested test class Prior to this commit, a dynamic property registered via a @DynamicPropertySource method in a @Nested test class was not able to override a property registered via a @DynamicPropertySource method in the enclosing class. See gh-26091 Closes gh-31083 --- ...namicPropertiesContextCustomizerFactory.java | 3 ++- .../DynamicPropertySourceNestedTests.java | 17 +++++++++++++++++ 2 files changed, 19 insertions(+), 1 deletion(-) diff --git a/spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java b/spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java index 11779cc12339..b0e636dc7e1c 100644 --- a/spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java +++ b/spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java @@ -35,6 +35,7 @@ * * @author Phillip Webb * @author Sam Brannen + * @author Yanming Zhou * @since 5.2.5 * @see DynamicPropertiesContextCustomizer */ @@ -54,10 +55,10 @@ public DynamicPropertiesContextCustomizer createContextCustomizer(Class testC } private void findMethods(Class testClass, Set methods) { - methods.addAll(MethodIntrospector.selectMethods(testClass, this::isAnnotated)); if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) { findMethods(testClass.getEnclosingClass(), methods); } + methods.addAll(MethodIntrospector.selectMethods(testClass, this::isAnnotated)); } private boolean isAnnotated(Method method) { diff --git a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DynamicPropertySourceNestedTests.java b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DynamicPropertySourceNestedTests.java index 08113cee82cd..06412e7eec87 100644 --- a/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DynamicPropertySourceNestedTests.java +++ b/spring-test/src/test/java/org/springframework/test/context/junit/jupiter/nested/DynamicPropertySourceNestedTests.java @@ -39,6 +39,7 @@ * {@link SpringExtension} in a JUnit Jupiter environment. * * @author Sam Brannen + * @author Yanming Zhou * @since 5.3.2 */ @SpringJUnitConfig @@ -125,6 +126,22 @@ void serviceHasInjectedValues(@Autowired Service service) { } } + @Nested + class DynamicPropertySourceOverrideEnclosingClassTests { + + @DynamicPropertySource + static void overrideDynamicPropertyFromEnclosingClass(DynamicPropertyRegistry registry) { + registry.add(TEST_CONTAINER_PORT, () -> -999); + } + + @Test + @DisplayName("@Service has values injected from @DynamicPropertySource in enclosing class and nested class") + void serviceHasInjectedValues(@Autowired Service service) { + assertThat(service.getIp()).isEqualTo("127.0.0.1"); + assertThat(service.getPort()).isEqualTo(-999); + } + + } static abstract class DynamicPropertySourceSuperclass {