Spring boot tests - unable to find test properties

I have a spring boot project and it works great. Now I want to write tests for my application, and I came across some configuration head heads.

Spring boot created a test class for me called ApplicationTests. It is very simple and looks like this:

@RunWith(SpringRunner.class) @SpringBootTest public class DuurzaamApplicationTests { @Test public void contextLoads() { } } 

Now when I run the tests, I get this error:

 java.lang.IllegalArgumentException: Could not resolve placeholder 'company.upload' in value "${company.upload}" 

I have a properties.yml file in the src / test / resources directory and for some reason it is not loaded. I tried all kinds of annotations from examples on the Internet, and yet none of them work.

How can I tell spring boot tests to use the application.yml file to load properties from?

+11
source share
4 answers

we can use @TestPropertySource or @PropertySource to load the properties file

 @RunWith(SpringRunner.class) @SpringBootTest @TestPropertySource("classpath:properties.yml") @ActiveProfiles("test") public class DuurzaamApplicationTests { @Test public void contextLoads() { } } 

Docs: https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/TestPropertySource.html.

+9
source

To my surprise, when you load property files in the Spring Boot Test, .yml not supported. This is noted in the documentation, albeit implicitly.

By the link above:

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/test/context/TestPropertySource.html

Supported File Formats

Both traditional and XML-based property file formats are supported - for example, "classpath: /com/example/test.properties" or "file: /path/to/file.xml".

.yml not mentioned.

And after changing my .yml to .properties and overwriting the values ​​in the form xx.xx.xx=value key-value pairs can be read correctly.

So strange.

+3
source

For me, the above solutions did not work, and any environment variables still overridden the test properties defined in @TestPropertySource, although https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features- external -config.html indicates that this source should have higher priority than environment variables. The only solution that worked for me was to manually define the PropertyPlaceholderConfigurer bean in the test configuration class and set it with the highest priority.

It was with Spring Boot 1.5.15.RELEASE

 @Configuration @TestPropertySource(properties = "/application-test.properties") @Slf4j public class IntegrationTestConfiguration { @Bean public static PropertyPlaceholderConfigurer properties() { PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer(); Resource[] resources = new ClassPathResource[] { new ClassPathResource( "/application-test.properties" ) }; ppc.setLocations( resources ); ppc.setIgnoreUnresolvablePlaceholders( true ); ppc.setOrder( Ordered.HIGHEST_PRECEDENCE ); return ppc; } /// .... @RunWith( SpringRunner.class ) @ActiveProfiles( "test" ) @Import( IntegrationTestConfiguration.class ) @SpringBootTest( webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT ) public class MyTest { 
0
source

@PropertySource and @TestPropertySource do not work with YAML. Watch this .

I also checked it myself. Try to create 2 files - * .yml and * .properties and see for yourself.

Most people use @SpringBootTest to get *.yml to work, but if that’s not what you want and instead you want to use @ContextConfiguration , you will be a little surprised.

0
source

Source: https://habr.com/ru/post/1270827/


All Articles