Spring Boot provides ways to register properties dynamically within the application context. This helps to:
Enhance Integration Testing: Particularly useful for integration tests utilizing Testcontainers, we can set properties based on container details dynamically.
Flexibility: Retrieve property values from external resources like databases or environment variables during runtime.
Implement Custom Logic: Design custom logic for determining property values, enabling complex test configurations or dynamic behavior adjustments.
Two key annotations for dynamic property registration are:
@DynamicPropertySource : Used to register properties dynamically within the test class, particularly useful for integration tests. It is a method-level annotation.
@TestPropertySource : Not strictly for dynamic properties, but rather for overriding application properties specifically within the integration tests or for creating new static properties. It is a class-level annotation.
Both @TestPropertySource and @DynamicPropertySource are processed before the Spring application context is loaded for the integration test. This ensures that the properties defined in these annotations are available for use by the test beans and components.