How to get the path of src/test/resources directory in JUnit?

前端 未结 14 1794
时光取名叫无心
时光取名叫无心 2020-11-28 01:25

I know I can load a file from src/test/resources with:

getClass().getResource(\"somefile\").getFile()

But how can I get the full path to th

14条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-11-28 01:51

    I have a Maven3 project using JUnit 4.12 and Java8. In order to get the path of a file called myxml.xml under src/test/resources, I do this from within the test case:

    @Test
    public void testApp()
    {
        File inputXmlFile = new File(this.getClass().getResource("/myxml.xml").getFile());
        System.out.println(inputXmlFile.getAbsolutePath());
        ...
    }
    

    Tested on Ubuntu 14.04 with IntelliJ IDE. Reference here.

提交回复
热议问题