Skip to content

Commit

Permalink
Allow overriding dynamic property from enclosing class in nested test…
Browse files Browse the repository at this point in the history
… 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
  • Loading branch information
quaff authored and sbrannen committed Aug 21, 2023
1 parent 8a6c0cd commit 368036c
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
Expand Up @@ -35,6 +35,7 @@
*
* @author Phillip Webb
* @author Sam Brannen
* @author Yanming Zhou
* @since 5.2.5
* @see DynamicPropertiesContextCustomizer
*/
Expand All @@ -54,10 +55,10 @@ public DynamicPropertiesContextCustomizer createContextCustomizer(Class<?> testC
}

private void findMethods(Class<?> testClass, Set<Method> 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) {
Expand Down
Expand Up @@ -39,6 +39,7 @@
* {@link SpringExtension} in a JUnit Jupiter environment.
*
* @author Sam Brannen
* @author Yanming Zhou
* @since 5.3.2
*/
@SpringJUnitConfig
Expand Down Expand Up @@ -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 {

Expand Down

0 comments on commit 368036c

Please sign in to comment.