It seems that @Nested and @DynamicPropertySource do not play together when both outer and inner classes have a source defined. Neither @DirtiesContext nor inheriting / overwritting seems to do the trick, (overwritting expects a full setup, while inheriting has the methods called in proper order but they don't seem to get applied).
I've added a manual test to the existing test class that should fail as I'd expect "1" to be the host but it is not.
SpringJUnitConfig
class DynamicPropertySourceNestedTests {
private static final String TEST_CONTAINER_IP = "DynamicPropertySourceNestedTests.test.container.ip";
private static final String TEST_CONTAINER_PORT = "DynamicPropertySourceNestedTests.test.container.port";
static DemoContainer container = new DemoContainer();
@DynamicPropertySource
static void containerProperties(DynamicPropertyRegistry registry) {
registry.add(TEST_CONTAINER_IP, container::getIpAddress);
registry.add(TEST_CONTAINER_PORT, container::getPort);
}
private static void assertServiceHasInjectedValues(Service service) {
assertThat(service.getIp()).isEqualTo("127.0.0.1");
assertThat(service.getPort()).isEqualTo(4242);
}
@Test
@DisplayName("@Service has values injected from @DynamicPropertySource")
void serviceHasInjectedValues(@Autowired Service service) {
assertServiceHasInjectedValues(service);
}
@Nested
class Testing {
@DynamicPropertySource
static void containerProperties(DynamicPropertyRegistry registry) {
registry.add(TEST_CONTAINER_IP, () -> "1"); // even though it is called after outer dynamic properties it does not get applied
registry.add(TEST_CONTAINER_PORT, container::getPort);
}
@Test
void serviceHasInjectedValues(@Autowired Service service) {
assertServiceHasInjectedValues(service);
}
}
@Configuration
@Import(Service.class)
static class Config {
}
static class Service {
private final String ip;
private final int port;
Service(@Value("${" + TEST_CONTAINER_IP + ":10.0.0.1}") String ip, @Value("${" + TEST_CONTAINER_PORT + ":-999}") int port) {
this.ip = ip;
this.port = port;
}
String getIp() {
return this.ip;
}
int getPort() {
return this.port;
}
}
static class DemoContainer {
String getIpAddress() {
return "127.0.0.1";
}
int getPort() {
return 4242;
}
}
}
Comment From: quaff
It's belong to spring-framework not spring-boot, I've created https://github.com/spring-projects/spring-framework/pull/31083 to fix it.
Comment From: bclozel
Superseded by https://github.com/spring-projects/spring-framework/pull/31083