当我尝试用作测试资源时无法加载我的文件

My file can't be loaded when i try to used as test resources

这是我的项目结构:我有一个 xml file 需要和我的 junit test case 一起使用,我不知道为什么我不能得到这个文件。

连我用intellij把目录标记为测试目录资源也没办法。

这是我的方法:

@Before
    public void init() throws Exception {

        xml = IOUtils.toString(
                this.getClass().getResourceAsStream("reponse.xml"),
                "UTF-8"
        );
        ......
    }

我总是得到相同的错误跟踪:

java.lang.NullPointerException
        at java.io.Reader.<init>(Reader.java:78)
        at java.io.InputStreamReader.<init>(InputStreamReader.java:113)
        at org.apache.commons.io.IOUtils.copy(IOUtils.java:1906)
        at org.apache.commons.io.IOUtils.toString(IOUtils.java:778)
        at org.apache.commons.io.IOUtils.toString(IOUtils.java:803)
        at fr.gouv.crpcen.restitution.ParserServiceTest.init(ParserServiceTest.java:113)

尝试将 "reponse.xml" 更改为“/reponse.xml”

你漏掉了一些小细节,没有这个东西是行不通的。

这里是固定版本:

@Before
public void init() throws Exception {

    xml = IOUtils.toString(
            this.getClass().getResourceAsStream("/response.xml"),
            "UTF-8"
    );
    ......
}

我检查过它很有魅力。