diff --git a/spring-test/src/main/java/org/springframework/test/context/DynamicPropertySource.java b/spring-test/src/main/java/org/springframework/test/context/DynamicPropertySource.java index b2c9f27a5c..518ce44cb8 100644 --- a/spring-test/src/main/java/org/springframework/test/context/DynamicPropertySource.java +++ b/spring-test/src/main/java/org/springframework/test/context/DynamicPropertySource.java @@ -39,7 +39,14 @@ import java.lang.annotation.Target; * {@code PropertySources}. Values are dynamic and provided via a * {@link java.util.function.Supplier} which is only invoked when the property * is resolved. Typically, method references are used to supply values, as in the - * following example. + * example below. + * + *
NOTE: if you use {@code @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 + * {@link org.springframework.test.annotation.DirtiesContext @DirtiesContext} to + * ensure that each subclass gets its own {@code ApplicationContext} with the + * correct dynamic properties. * *
Dynamic properties have higher precedence than those loaded from
@@ -77,6 +84,7 @@ import java.lang.annotation.Target;
* @see ContextConfiguration
* @see TestPropertySource
* @see org.springframework.core.env.PropertySource
+ * @see org.springframework.test.annotation.DirtiesContext
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
diff --git a/src/docs/asciidoc/testing.adoc b/src/docs/asciidoc/testing.adoc
index 4c53b749bc..9738ec8752 100644
--- a/src/docs/asciidoc/testing.adoc
+++ b/src/docs/asciidoc/testing.adoc
@@ -4093,11 +4093,14 @@ integration tests that need to add properties with dynamic values to the set of
`PropertySources` in the `Environment` for the `ApplicationContext` loaded for the
integration test.
-NOTE: The `@DynamicPropertySource` annotation and its supporting infrastructure were
+[NOTE]
+====
+The `@DynamicPropertySource` annotation and its supporting infrastructure were
originally designed to allow properties from
https://www.testcontainers.org/[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`.
+====
In contrast to the <