@@ -3947,7 +3947,6 @@ to define properties in both a subclass and its superclass by using inline prope
3947
3947
// ...
3948
3948
}
3949
3949
----
3950
-
3951
3950
[source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"]
3952
3951
.Kotlin
3953
3952
----
@@ -3964,6 +3963,54 @@ to define properties in both a subclass and its superclass by using inline prope
3964
3963
}
3965
3964
----
3966
3965
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
+
3967
4014
[[testcontext-ctx-management-web]]
3968
4015
===== Loading a `WebApplicationContext`
3969
4016
0 commit comments