Annotation Interface DynamicPropertySource
@DynamicPropertySource is an annotation that can be applied to static
methods in integration test classes or to @Bean methods in test
@Configuration classes in order to add properties with dynamic values
to the 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 be used
with any form of external resource whose lifecycle is managed outside the
test's ApplicationContext or with beans whose lifecycle is managed by
the test's ApplicationContext.
@DynamicPropertySource-annotated 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.
@Bean methods annotated with @DynamicPropertySource may
either accept an argument of type DynamicPropertyRegistry or access a
DynamicPropertyRegistry instance autowired into their enclosing
@Configuration class. Note, however, that @Bean methods which
interact with a DynamicPropertyRegistry are not required to be annotated
with @DynamicPropertySource unless they need to enforce eager
initialization of the bean within the context.
See DynamicPropertyRegistry for details.
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);
}
}
The following example demonstrates how to use @DynamicPropertySource
with a @Bean method. Beans in the ApplicationContext can
access the api.url property which is dynamically retrieved from the
ApiServer bean.
@Configuration
class TestConfig {
@Bean
@DynamicPropertySource
ApiServer apiServer(DynamicPropertyRegistry registry) {
ApiServer apiServer = new ApiServer();
registry.add("api.url", apiServer::getUrl);
return apiServer;
}
}- Since:
- 5.2.5
- Author:
- Phillip Webb, Sam Brannen
- See Also: