Document @DynamicPropertySource support in the reference manual
Closes gh-24540
This commit is contained in:
		
							parent
							
								
									a90716522c
								
							
						
					
					
						commit
						ac0363c94e
					
				|  | @ -3947,7 +3947,6 @@ to define properties in both a subclass and its superclass by using inline prope | |||
| 		// ... | ||||
| 	} | ||||
| ---- | ||||
| 
 | ||||
| [source,kotlin,indent=0,subs="verbatim,quotes",role="secondary"] | ||||
| .Kotlin | ||||
| ---- | ||||
|  | @ -3964,6 +3963,54 @@ to define properties in both a subclass and its superclass by using inline prope | |||
| 	} | ||||
| ---- | ||||
| 
 | ||||
| [[testcontext-ctx-management-dynamic-property-sources]] | ||||
| ===== Context Configuration with Dynamic Property Sources | ||||
| 
 | ||||
| As of Spring Framework 5.2.5, the TestContext framework provides support for _dynamic_ | ||||
| property sources via the `@DynamicPropertySource` annotation. This annotation can be used | ||||
| in 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 | ||||
| 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 <<testcontext-ctx-management-property-sources,`@TestPropertySource`>> | ||||
| annotation that is applied at the class level, `@DynamicPropertySource` must be applied | ||||
| to a `static` method that accepts a single `DynamicPropertyRegistry` argument which is | ||||
| used to add _name-value_ pairs to the `Environment`. 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 can be seen in the following example which uses | ||||
| the Testcontainers project to manage a Redis container outside of the Spring | ||||
| `ApplicationContext`. The IP address and port of the managed Redis container are made | ||||
| available to components within the test's `ApplicationContext` via the `redis.host` and | ||||
| `redis.port` properties. These properties can be injected into Spring-managed components | ||||
| via `@Value("${redis.host}")` and `@Value("${redis.port}")`, respectively. | ||||
| 
 | ||||
| [source,java,indent=0,subs="verbatim,quotes",role="primary"] | ||||
| .Java | ||||
| ---- | ||||
| 	@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); | ||||
| 		} | ||||
| 
 | ||||
| 		// tests ... | ||||
| 
 | ||||
| 	} | ||||
| ---- | ||||
| 
 | ||||
| [[testcontext-ctx-management-web]] | ||||
| ===== Loading a `WebApplicationContext` | ||||
| 
 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue