-
Notifications
You must be signed in to change notification settings - Fork 40.9k
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? |
aa2179a
to
b47e010
Compare
We actually considered that back when we introduced I also implemented a custom You can see it in action in The reason we didn't make that an official feature of the Spring TestContext Framework is that we doubted that it would meet the needs of enough developers. The rationale was that Framework's YAML support in
What kind of hook point did you have in mind, and who would hook into that? |
If you added some kind of YAML support, we could have a |
Adds @TestYamlPropertySource annotation which provides a convenient alternative for @TestPropertySource(factory=YamlPropertySourceFactory.class). Adds YamlPropertySourceFactory to load YAML files into the Environment through @TestPropertySource or @TestYamlPropertySource. Signed-off-by: Dmytro Nosan <[email protected]>
If it's decided to include the proposed support (or a variant thereof) in Spring Boot, instead of |
Honestly, the more I think about this PR, the more I want to close it. If support for As a compromise, public class YamlPropertySourceFactory implements PropertySourceFactory {
private static final YamlPropertySourceLoader loader = new YamlPropertySourceLoader();
@Override
public PropertySource<?> createPropertySource(String name, EncodedResource encodedResource) throws IOException {
Resource resource = encodedResource.getResource();
String propertySourceName = getPropertySourceName(name, resource);
List<PropertySource<?>> propertySources = loader.load(propertySourceName, resource);
CompositePropertySource compositePropertySource = new CompositePropertySource(propertySourceName);
propertySources.forEach(compositePropertySource::addFirstPropertySource);
return compositePropertySource;
}
private static String getPropertySourceName(String name, Resource resource) {
if (StringUtils.hasText(name)) {
return name;
}
String description = resource.getDescription();
if (StringUtils.hasText(description)) {
return description;
}
return resource.getClass().getSimpleName() + "@" + System.identityHashCode(resource);
}
} And if the provided spring:
bar: bar
foo: baz
---
spring:
foo: foo The values from the later document replace those from the earlier one. spring.bar=bar
spring.foo=foo This behaviour will be similar to The usage of this factory will not be verbose: @TestPropertySource(locations = { "test.yaml", "test1.yaml" }, factory = YamlPropertySourceFactory.class)
class YamlTestPropertySourceIntegrationTests { } From my perspective, it is the best option. |
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.