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

Warn that multi-document property files cannot be loaded by using @PropertySource or @TestPropertySource #24945

Closed
peter-thomas-mgd opened this issue Jan 20, 2021 · 2 comments
Labels
type: documentation A documentation update
Milestone

Comments

@peter-thomas-mgd
Copy link

Expected behavior is that a multi-document properties file can be created with different sections separated by "#--- " characters, for each section applicable to a given active profile. This works as expected when using a file named application.properties and allowing Spring Boot to bootstrap the application normally.

However, if the default application properties file is overridden in a JUnit test class annotated with a test property source (e.g. @TestPropertySource(locations="classpath:test.properties")), Spring Boot ignores the active profiles and simply references the last value it finds for the property in the properties file.

I have created a sample Java project to demonstrate the issue here: https://github.com/peter-thomas-mgd/spring-multi-doc

The example shows how an identical properties file behaves differently when specified as a test property source called test.properties vs. when bootstrapped as the default application.properties.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Jan 20, 2021
@philwebb
Copy link
Member

This is a little annoying, but unfortunately @TestPropertySource is part of the Spring Framework codebase and as such doesn't know anything about multi-document property files.

I'm not sure on the best way to fix this. We could document it as a limitation, or we could perhaps develop an alternative annotation. It's a tricky problem because there are other aspects (such as spring.config.import) that also won't work.

In the meantime, you might want to look at @SpringBootTest or SpringBootContextLoader which uses SpringApplication and so should load the full application.properties file.

@philwebb philwebb added the for: team-attention An issue we'd like other members of the team to review label Jan 20, 2021
@peter-thomas-mgd
Copy link
Author

Thanks for responding so quickly. We have already implemented a workaround, which is using @SpringBootTest exactly as you suggest, and moving our properties to application.properties. However, I wanted to make sure this issue got on the radar, since I'm sure other teams are going to run into this issue at some point.

@philwebb philwebb added type: documentation A documentation update and removed for: team-attention An issue we'd like other members of the team to review status: waiting-for-triage An issue we've not yet triaged labels Jan 25, 2021
@philwebb philwebb added this to the 2.4.x milestone Jan 25, 2021
@mbhave mbhave modified the milestones: 2.4.x, 2.4.4 Mar 4, 2021
@mbhave mbhave closed this as completed in ae630e0 Mar 4, 2021
@wilkinsona wilkinsona changed the title Multi-document behavior breaks if properties file is referenced using @TestPropertySource Warn that multi-document property files cannot be loaded by using @PropertySource or @TestPropertySource Mar 5, 2021
This was referenced Mar 18, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: documentation A documentation update
Projects
None yet
Development

No branches or pull requests

4 participants