Spring-boot: Map properties in yaml from TestPropertySource not working in boot 2.0.0.Mx

Created on 25 Oct 2017  路  9Comments  路  Source: spring-projects/spring-boot

In this example if you only change the boot version in the pom file and nothing else then the test goes from passing in 1.5.x to failing in 2.0.0.Mx:

https://github.com/ryandawsonuk/nest-map-spring-boot-config

As the example explains, the error is ConverterNotFoundException. It only happens when the file is referenced via TestPropertySource, not when application.yml in src/main/resources is used or even if an application.yml in src/test/resources is used.

invalid

Most helpful comment

meanwhile i found a workaround:
Use @SpringBootTest annotation and use classpath in your TestPropertySource:

@SpringBootTest
@TestPropertySource(properties = {"spring.config.location = classpath:test.yaml"})

All 9 comments

@ryandawsonuk thanks for the sample.

@TestPropertySource seems like it is meant to be used with .properties files and not .yaml files. It treats the yaml file as a properties file and creates a source that has properties with keys activiti, cloud, user etc instead of a property with key activiti.cloud.user.bob.policy.read. In 1.5, it didn't throw an exception but it wasn't really using the properties from the TestPropertySource. Changing activiti.cloud.user.bob.policy.read to a different value in application.yml causes the test to fail.
In 2.0, the new binder finds a property for activity with value "", tries to convert that to a ActivitiesProperty and fails with a conversion exception.

@snicoll Can you confirm that @TestPropertySource was not meant to be used with yaml files?

@PropertySource and TestPropertySource do not work with YAML indeed.

at least document it properly in the java doc ?

Yet i think its should be supported as its obvious for any spring user that YAML files are 1st class citizens !

@mistriel Both @PropertySource and TestPropertySource are part of Spring Framework rather than Spring Boot. If you would like to pursue your suggestion, please open a Spring Framework issue.

I see.
Thanks for the clarification.
Yet, IMHO a sibling boot annotation is expected WDYT ?

@mistriel Thanks for the suggestion, but I don't think adding a YAML-specific annotation to Spring Boot is the right solution here. If YAML support is going to be added then I think it should be added to Spring Framework. https://github.com/spring-projects/spring-framework/issues/18486 is tracking that. You may want to comment there to see if it can be re-opened or some amendments made to the javadoc.

@wilkinsona Thanks for the reference.
I will try to push this in the other thread.

meanwhile i found a workaround:
Use @SpringBootTest annotation and use classpath in your TestPropertySource:

@SpringBootTest
@TestPropertySource(properties = {"spring.config.location = classpath:test.yaml"})

I ended up using @PropertySource instead since it now supports a factory attribute.
https://www.baeldung.com/spring-yaml-propertysource

@ExtendWith(SpringExtension::class)
@EnableConfigurationProperties(value = [ServiceConfigs::class])
@PropertySource("classpath:service-config-test.yml", factory = YamlPropertySourceFactory::class)
Was this page helpful?
0 / 5 - 0 ratings

Related issues

ganeshkrishnan1 picture ganeshkrishnan1  路  63Comments

philwebb picture philwebb  路  45Comments

igormukhin picture igormukhin  路  55Comments

snicoll picture snicoll  路  37Comments

berlin-ab picture berlin-ab  路  33Comments