DynamicPropertyRegistry was introduced in conjunction with the @DynamicPropertySource support in the TestContext framework. For such use cases, a static method within a test class must be annotated with @DynamicPropertySource, and a DynamicPropertyRegistry will be supplied as a method argument.
However, it can also be useful to be able to register a "dynamic property" from within a test's ApplicationContext -- for example, in a @Bean method in a @Configuration class that is specific to testing scenarios.
In light of that, we will register DynamicPropertyRegistry as a singleton bean in a test's ApplicationContext. That will allow DynamicPropertyRegistry to be autowired into a @Configuration class or supplied to a @Bean method as a method argument as shown in the following example.
In order to address the @DependsOn concern raised in #32209, we are considering allowing @DynamicPropertySource to be applied to @Bean methods to indicate that the corresponding bean should be eagerly initialized within the test's ApplicationContext.
Overview
DynamicPropertyRegistry
was introduced in conjunction with the@DynamicPropertySource
support in the TestContext framework. For such use cases, astatic
method within a test class must be annotated with@DynamicPropertySource
, and aDynamicPropertyRegistry
will be supplied as a method argument.However, it can also be useful to be able to register a "dynamic property" from within a test's
ApplicationContext
-- for example, in a@Bean
method in a@Configuration
class that is specific to testing scenarios.In light of that, we will register
DynamicPropertyRegistry
as a singleton bean in a test'sApplicationContext
. That will allowDynamicPropertyRegistry
to be autowired into a@Configuration
class or supplied to a@Bean
method as a method argument as shown in the following example.In order to address the
@DependsOn
concern raised in #32209, we are considering allowing@DynamicPropertySource
to be applied to@Bean
methods to indicate that the corresponding bean should be eagerly initialized within the test'sApplicationContext
.Related Issues
32209