How to read src/test/resources/ file in Junit in Spring Boot ?

This is the very short program which explains how to read the xml/propery files presents inside the src/test/resources folder in your spring boot project.

Reading xml file from src/test/resources:

Below code reads the file from src/test/resources/my_sample_xml.xml file content and just prints in the console.

@SpringBootTest
public class XmlPropertyRead{


    @Test
    public void testXmlAttribute() throws Exception {
        Path relPath = Paths.get("src", "test", "resources", "my_sample_xml.xml");
        String content = null;
        try {
            content =Files.lines(relPath).collect(Collectors.joining(System.lineSeparator()));
            System.out.println(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
        assertTrue(true);
    }

}

Leave a Reply