Add YamlPropertySourceFactory that can be used for loading YAML files through the @TestPropertySource annotation #42603
+206
−1
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I have prepared two potential solutions for this #33434 enhancement.
The first approach offers a simple and intuitive way to use it:
YamlPropertySourceFactory
loads all properties from the specified YAML file usingYamlPropertySourceLoader
and returns aCompositePropertySource
containing the loaded properties.As a potential improvement, a dedicated annotation like
@TestYamlPropertySource
could be introduced to simplify the loading of YAML files in tests.With a dedicated annotation, the following syntax could be supported:
An alternative approach is to customize
PropertySourceDescriptor
inSpringBootTestContextBootstrapper
by using thePropertySourceLoaderPropertySourceFactory
, which leverages the existingPropertySourceLoader
. With that approach, the following syntax would be possible:But it has several disadvantages:
It works only with the
@SpringBootTest
annotation. For@SpringJUnitConfig
, the factory needs to be explicitly used.It does not support
EncodedResource
for .properties files becausePropertySourceLoader
only works with Resource objects. As a result, if someone is using@TestPropertySource
with the encoding attribute, these changes could potentially impact them.Not obvious.
The first approach is much better and straightforward, and people would not have any problem using it.