Reproduced with Spring Framework 5.3.1 (via Spring Boot 2.4.0).

As initially described here and here, nested tests do not work with @DynamicPropertySource.

You can reproduce the issue with this sample Spring Data JPA (2.4.1) repository: https://github.com/fbiville/spring-data-jpa-nested (see README for reproduction one-liners).

As suggested by @wilkinsona here, replacing the annotation with ApplicationContextInitializer works.

Comment From: sbrannen

Thanks so much for raising the issue!

When implementing #19930, my goal was to provide support for inheriting enclosing class configuration for all class-level annotations; however, I simply overlooked @DynamicPropertySource in the process.

@DynamicPropertySource is technically a static method-level annotation, but it in fact contributes to the context cache key in the Spring TestContext Framework.

So I apologize for missing that.

I've already reproduced the issue in an integration test using only the Spring Framework, and the fix requires some changes in DynamicPropertiesContextCustomizerFactory to honor @NestedTestConfiguration semantics. So I'll make sure to provide proper support for inheriting @DynamicPropertySource configuration in @Nested test classes in Spring Framework 5.3.2.

Comment From: sbrannen

I've added support for inheriting @DynamicPropertySource methods from enclosing classes.

This will be released with Spring Framework 5.3.2, but feel free to try it out in an upcoming snapshot.

As a workaround for 5.3.0 and 5.3.1, you can create a test base class (typically abstract) or test interface that only includes the @DynamicPropertySource method and then extend or implement that class or interface in each @Nested test class that needs it. Examples can be seen in the DynamicPropertySourceFromSuperclassTests and DynamicPropertySourceFromInterfaceTests nested test classes in DynamicPropertySourceNestedTests.