@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.
Dynamic properties have higher precedence than those loaded from
@TestPropertySource
, the operating system's
environment, Java system properties, or property sources added by the
application declaratively by using
@PropertySource
or programmatically. Thus, dynamic properties can be used to selectively
override properties loaded via @TestPropertySource
, system property
sources, and application property sources.
@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