Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
sbrannen opened this issue Feb 14, 2024 · 1 comment
Assignees
Labels
in: test Issues in the test module type: enhancement A general enhancement
Milestone

Comments

@sbrannen
Copy link
Member

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

@sbrannen sbrannen added in: test Issues in the test module type: enhancement A general enhancement labels Feb 14, 2024
@sbrannen sbrannen added this to the 6.2.0-M1 milestone Feb 14, 2024
@sbrannen sbrannen self-assigned this Feb 14, 2024
@sbrannen
Copy link
Member Author

A proof of concept can be viewed in the following feature branch.

main...sbrannen:spring-framework:issues/gh-32209-register-DynamicPropertyRegistry-as-bean

@sbrannen sbrannen modified the milestones: 6.2.0-M1, 6.2.0-M2 Apr 9, 2024
sbrannen added a commit that referenced this issue May 22, 2024
Prior to this commit, as a result of commit 6cdb344, the
DynamicValuesPropertySource was eagerly registered in the Environment
even if the DynamicPropertyRegistry was never used to register dynamic
properties.

This commit ensures that the DynamicValuesPropertySource is only
registered if we know that the DynamicPropertyRegistry is actually used
-- either by a @⁠DynamicPropertySource method in a test class or via a
bean in the ApplicationContext that invokes add() on the
DynamicPropertyRegistry bean.

See gh-32271
Closes gh-32871
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: test Issues in the test module type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

1 participant