From 368036cab433e6f4b1dc1045725a2677e4400d70 Mon Sep 17 00:00:00 2001 From: Yanming Zhou Date: Mon, 21 Aug 2023 17:37:57 +0800 Subject: [PATCH 1/2] 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 11779cc1233..b0e636dc7e1 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 @@ import org.springframework.test.context.TestContextAnnotationUtils; * * @author Phillip Webb * @author Sam Brannen + * @author Yanming Zhou * @since 5.2.5 * @see DynamicPropertiesContextCustomizer */ @@ -54,10 +55,10 @@ class DynamicPropertiesContextCustomizerFactory implements ContextCustomizerFact } 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 08113cee82c..06412e7eec8 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 @@ import static org.springframework.test.context.NestedTestConfiguration.Enclosing * {@link SpringExtension} in a JUnit Jupiter environment. * * @author Sam Brannen + * @author Yanming Zhou * @since 5.3.2 */ @SpringJUnitConfig @@ -125,6 +126,22 @@ class DynamicPropertySourceNestedTests { } } + @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 { From 620a87bcbc966be0c71a3c66c78c5633936f66c8 Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 21 Aug 2023 14:17:05 +0200 Subject: [PATCH 2/2] Polishing --- .../support/DynamicPropertiesContextCustomizerFactory.java | 2 +- .../jupiter/nested/DynamicPropertySourceNestedTests.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) 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 b0e636dc7e1..1f8e531a76d 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 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. 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 06412e7eec8..7737d7c9f56 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2023 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. @@ -127,7 +127,7 @@ class DynamicPropertySourceNestedTests { } @Nested - class DynamicPropertySourceOverrideEnclosingClassTests { + class DynamicPropertySourceOverridesEnclosingClassTests { @DynamicPropertySource static void overrideDynamicPropertyFromEnclosingClass(DynamicPropertyRegistry registry) {