Skip to content

Register DynamicPropertyRegistry as a singleton bean in a test's ApplicationContext #32271

Closed
@sbrannen

Description

@sbrannen

Overview

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.

@Bean
ApiServer apiServer(DynamicPropertyRegistry registry) {
	ApiServer apiServer = new ApiServer();
	registry.add("api.url", apiServer::getUrl);
	return apiServer;
}

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.

Related Issues

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions