Skip to content

Commit 9894174

Browse files
quaffsbrannen
authored andcommitted
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
1 parent 493f75e commit 9894174

File tree

2 files changed

+19
-1
lines changed

2 files changed

+19
-1
lines changed

spring-test/src/main/java/org/springframework/test/context/support/DynamicPropertiesContextCustomizerFactory.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
*
3636
* @author Phillip Webb
3737
* @author Sam Brannen
38+
* @author Yanming Zhou
3839
* @since 5.2.5
3940
* @see DynamicPropertiesContextCustomizer
4041
*/
@@ -54,10 +55,10 @@ public DynamicPropertiesContextCustomizer createContextCustomizer(Class<?> testC
5455
}
5556

5657
private void findMethods(Class<?> testClass, Set<Method> methods) {
57-
methods.addAll(MethodIntrospector.selectMethods(testClass, this::isAnnotated));
5858
if (TestContextAnnotationUtils.searchEnclosingClass(testClass)) {
5959
findMethods(testClass.getEnclosingClass(), methods);
6060
}
61+
methods.addAll(MethodIntrospector.selectMethods(testClass, this::isAnnotated));
6162
}
6263

6364
private boolean isAnnotated(Method method) {

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* {@link SpringExtension} in a JUnit Jupiter environment.
4040
*
4141
* @author Sam Brannen
42+
* @author Yanming Zhou
4243
* @since 5.3.2
4344
*/
4445
@SpringJUnitConfig
@@ -125,6 +126,22 @@ void serviceHasInjectedValues(@Autowired Service service) {
125126
}
126127
}
127128

129+
@Nested
130+
class DynamicPropertySourceOverrideEnclosingClassTests {
131+
132+
@DynamicPropertySource
133+
static void overrideDynamicPropertyFromEnclosingClass(DynamicPropertyRegistry registry) {
134+
registry.add(TEST_CONTAINER_PORT, () -> -999);
135+
}
136+
137+
@Test
138+
@DisplayName("@Service has values injected from @DynamicPropertySource in enclosing class and nested class")
139+
void serviceHasInjectedValues(@Autowired Service service) {
140+
assertThat(service.getIp()).isEqualTo("127.0.0.1");
141+
assertThat(service.getPort()).isEqualTo(-999);
142+
}
143+
144+
}
128145

129146
static abstract class DynamicPropertySourceSuperclass {
130147

0 commit comments

Comments
 (0)