Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Spring boot @PropertySource not working for yaml, but working for properties

I need help in changing TestPropertySource for my integration tests in spring boot
Previously I was using properties which were working fine, now i thought of changing to yaml, because of hierarchical structure and clean.

I am not not able to switch to application-test.yaml for integration tests.
I tried
Spring @PropertySource using YAML
But it didnt work, my test yaml is not getting called

@PropertySource(value = "classpath:application-test.yaml", factory = YamlPropertyLoaderFactory.class)

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

even changing like this didnt work

@TestPropertySource(properties = { "spring.config.location = classpath:<path-to-your-yaml-file>" }

This below is example of my integration test for globalExceptionHandler which worked fine with properties

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@TestPropertySource(locations = "/application-test.properties")
@AutoConfigureMockMvc
public class GlobalExceptionHandlerIntTests {
    @Autowired
    private MockMvc mockMvc;

    @Test
    @DisplayName("Bad request when invalid data sent")
    public void whenPostInvalidData_thenBadRequest() throws Exception {
        String invalidDataJson = "{\"name\":\"\",\"email\":-1,\"password\":\"\"}";

        mockMvc.perform(MockMvcRequestBuilders.post("/signup")
                        .content(invalidDataJson)
                        .contentType(MediaType.APPLICATION_JSON))
                .andExpect(status().isBadRequest())
                .andExpect(jsonPath("$.status").value(400))
                ...
                ...

>Solution :

AFAIK the current status of the TestPropertySource is that it does not recognize YAMLs,

https://github.com/spring-projects/spring-boot/issues/33434

Remember that annotations just inject (magical) code, so in the end if the supporting classes of the annotation don’t do what you expect them to do it is a no-no.

I would try to create a profile for your tests (it only changes the way to run it), check this answer

Can't load a test-properties.yaml file with the @TestPropertySource with Spring Boot, JUnit5 and Kotlin

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading