注释类型 DynamicPropertySource
@Target(METHOD) @Retention(RUNTIME) @Documented public @interface DynamicPropertySource
Method-level annotation for integration tests that need to add properties with dynamic values to theEnvironment
's set ofPropertySources
.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 bestatic
and must have a singleDynamicPropertyRegistry
argument which is used to add name-value pairs to theEnvironment
's set ofPropertySources
. Values are dynamic and provided via aSupplier
which is only invoked when the property is resolved. Typically, method references are used to supply values, as in the following example.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.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); } }
- 从以下版本开始:
- 5.2.5
- 作者:
- Phillip Webb, Sam Brannen
- 另请参阅:
DynamicPropertyRegistry
,ContextConfiguration
,TestPropertySource
,PropertySource