@Target(value=METHOD) @Retention(value=RUNTIME) @Documented public @interface DynamicPropertySource
Environment's set of PropertySources.
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.
Methods annotated with @DynamicPropertySource must be static
and must have a single DynamicPropertyRegistry argument which is used
to add name-value pairs to the Environment's set of
PropertySources. Values are dynamic and provided via a Supplier
which is only invoked when the property is resolved. Typically, method references
are used to supply values, as in the following 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);
}
}DynamicPropertyRegistry,
ContextConfiguration,
TestPropertySource,
PropertySource