Spring boot tests - Can't find test properties

We can use @TestPropertySource or @PropertySource to load the properties file.

Example:

@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


To my surprise, when you load properties files in Spring Boot Test, .yml is not supported. It's noted in the documentation, although implicitly.

From 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 properties file formats are supported — for example, "classpath:/com/example/test.properties" or "file:/path/to/file.xml".

.yml is not mentioned.

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

So strange.

EDIT:

Now I find a ticket address this issue; seems a long-known bug in Spring.

https://github.com/spring-projects/spring-framework/issues/18486


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

I also tested it myself. Try creating 2 files - *.yml and *.properties and see it for yourself.

To make *.yml work most people use @SpringBootTest, but if it's not what you want and you would like to use @ContextConfiguration instead, you are in for a bit of surprise.