The property @DynamicPropertySource was added as part of the release 5.2.5 of the Spring Framework. The official documentation says that:
This annotation and its supporting infrastructure were originally designed to allow properties from Testcontainers based tests to be exposed easily to Spring integration tests. However, this feature may also be used with any form of external resource whose lifecycle is maintained outside the test's ApplicationContext.
There is also a basic example:
@SpringJUnitConfig(...)
@Testcontainers
class ExampleIntegrationTests {
@Container
static RedisContainer redis = new RedisContainer(); // ...
@DynamicPropertySource
static void redisProperties(DynamicPropertyRegistry registry) {
registry.add("redis.host", redis::getContainerIpAddress);
registry.add("redis.port", redis::getMappedPort);
} }
However, I don't understand... what's the use case for this new annotation when we have @PropertySource that behaves in the same way?