-
Notifications
You must be signed in to change notification settings - Fork 40.7k
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
Add YamlPropertySourceFactory that can be used for loading YAML files through the @TestPropertySource annotation #42603
base: main
Are you sure you want to change the base?
Conversation
cbeb1bd
to
b297962
Compare
The third option @SpringJUnitConfig
@TestPropertySource(locations = { "classpath:test.yaml", "classpath:test1.yaml", "classpath:test.properties" },
factory = PropertySourceLoaderPropertySourceFactory.class)
class PropertySourceLoaderPropertySourceFactoryIntegrationTests {
} |
I like |
I was trying to add this support, but to be honest, I have not found a way.
I think most people will use a dedicated YAML file to configure their test context, and I doubt anyone will try to use the
I completely agree, and it would be beneficial to include this information in the Javadoc and documentation. |
Perhaps we should make it a hard requirement that only a single document is in the YAML and that no profile restrictions or imports are specified. We'll discuss this on a team call when can. |
I'm wondering if basic YAML support should be something that's added to Spring Framework. Perhaps with some hook point so that we can warn if users accidentally point at YAML files that won't work. What do you think @sbrannen? |
535dc54
to
96dd0a8
Compare
that can be applied to a test class to add PropertySource loaded from YAML files to the Environment This commit introduces new functionality to load properties from YAML files in tests. The @TestYamlPropertySource annotation is added to allow test classes to specify YAML files as property sources. The @TestYamlPropertySource provides a convenient alternative for @TestPropertySource(factory=YamlPropertySourceFactory.class). The YamlPropertySourceFactory class is also introduced to enable the loading of YAML files into the Environment through @TestPropertySource.
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.