Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

@DynamicPropertySource in @Nested test class cannot override dynamic properties from enclosing class #31083

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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