Skip to content

Commit ac0363c

Browse files
committed
Document @DynamicPropertySource support in the reference manual
Closes gh-24540
1 parent a907165 commit ac0363c

File tree

1 file changed

+48
-1
lines changed

1 file changed

+48
-1
lines changed

src/docs/asciidoc/testing.adoc

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3947,7 +3947,6 @@ to define properties in both a subclass and its superclass by using inline prope
39473947
// ...
39483948
}
39493949
----
3950-
39513950
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
39523951
.Kotlin
39533952
----
@@ -3964,6 +3963,54 @@ to define properties in both a subclass and its superclass by using inline prope
39643963
}
39653964
----
39663965

3966+
[[testcontext-ctx-management-dynamic-property-sources]]
3967+
===== Context Configuration with Dynamic Property Sources
3968+
3969+
As of Spring Framework 5.2.5, the TestContext framework provides support for _dynamic_
3970+
property sources via the `@DynamicPropertySource` annotation. This annotation can be used
3971+
in integration tests that need to add properties with dynamic values to the set of
3972+
`PropertySources` in the `Environment` for the `ApplicationContext` loaded for the
3973+
integration test.
3974+
3975+
NOTE: The `@DynamicPropertySource` annotation and its supporting infrastructure were
3976+
originally designed to allow properties from
3977+
https://www.testcontainers.org/[Testcontainers] based tests to be exposed easily to
3978+
Spring integration tests. However, this feature may also be used with any form of
3979+
external resource whose lifecycle is maintained outside the test's `ApplicationContext`.
3980+
3981+
In contrast to the <<testcontext-ctx-management-property-sources,`@TestPropertySource`>>
3982+
annotation that is applied at the class level, `@DynamicPropertySource` must be applied
3983+
to a `static` method that accepts a single `DynamicPropertyRegistry` argument which is
3984+
used to add _name-value_ pairs to the `Environment`. Values are dynamic and provided via
3985+
a `Supplier` which is only invoked when the property is resolved. Typically, method
3986+
references are used to supply values, as can be seen in the following example which uses
3987+
the Testcontainers project to manage a Redis container outside of the Spring
3988+
`ApplicationContext`. The IP address and port of the managed Redis container are made
3989+
available to components within the test's `ApplicationContext` via the `redis.host` and
3990+
`redis.port` properties. These properties can be injected into Spring-managed components
3991+
via `@Value("${redis.host}")` and `@Value("${redis.port}")`, respectively.
3992+
3993+
[source,java,indent=0,subs="verbatim,quotes",role="primary"]
3994+
.Java
3995+
----
3996+
@SpringJUnitConfig(/* ... */)
3997+
@Testcontainers
3998+
class ExampleIntegrationTests {
3999+
4000+
@Container
4001+
static RedisContainer redis = new RedisContainer();
4002+
4003+
@DynamicPropertySource
4004+
static void redisProperties(DynamicPropertyRegistry registry) {
4005+
registry.add("redis.host", redis::getContainerIpAddress);
4006+
registry.add("redis.port", redis::getMappedPort);
4007+
}
4008+
4009+
// tests ...
4010+
4011+
}
4012+
----
4013+
39674014
[[testcontext-ctx-management-web]]
39684015
===== Loading a `WebApplicationContext`
39694016

0 commit comments

Comments
 (0)