Annotation Interface DynamicPropertySource
@DynamicPropertySource
is an annotation that can be applied to static
methods in integration test classes in order to add properties with dynamic
values to the Environment
's set of PropertySources
.
Alternatively, dynamic properties can be added to the Environment
by special beans in the test's ApplicationContext
. See
DynamicPropertyRegistrar
for details.
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 be used
with any form of external resource whose lifecycle is managed outside the
test's ApplicationContext
.
@DynamicPropertySource
methods use a DynamicPropertyRegistry
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
example below.
Methods in integration test classes that are annotated with
@DynamicPropertySource
must be static
and must accept a single
DynamicPropertyRegistry
argument.
Dynamic properties from methods annotated with @DynamicPropertySource
will be inherited from enclosing test classes, analogous to inheritance
from superclasses and interfaces. See
@NestedTestConfiguration
for details.
NOTE: if you use @DynamicPropertySource
in a base
class and discover that tests in subclasses fail because the dynamic properties
change between subclasses, you may need to annotate your base class with
@DirtiesContext
to
ensure that each subclass gets its own ApplicationContext
with the
correct dynamic properties.
Precedence
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.
Examples
The following example demonstrates how to use @DynamicPropertySource
in an integration test class. Beans in the ApplicationContext
can
access the redis.host
and redis.port
properties which are
dynamically retrieved from the Redis container.
@SpringJUnitConfig(...) @Testcontainers class ExampleIntegrationTests { @Container static GenericContainer redis = new GenericContainer("redis:5.0.3-alpine").withExposedPorts(6379); // ... @DynamicPropertySource static void redisProperties(DynamicPropertyRegistry registry) { registry.add("redis.host", redis::getHost); registry.add("redis.port", redis::getFirstMappedPort); } }
- Since:
- 5.2.5
- Author:
- Phillip Webb, Sam Brannen
- See Also: