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)
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))
...
...
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