maven测试找不到资源

luc*_*umi 3 testing resources maven

我正在尝试做 Maven 测试。

我已经设置了资源目录

<testResources>
        <testResource>
            <directory>src/test/resources</directory>
        </testResource>
    </testResources>
Run Code Online (Sandbox Code Playgroud)

我在这个目录中放置了一个名为 inbound 的文件夹。在我的 junit 中,我尝试将其作为新文件(“Inbound”)打开,但它总是找不到它。我的项目根目录下有一个入站副本。junit代码找不到它。有人可以给我一些建议吗?谢谢

Rom*_*las 5

如果您使用src/test/resources,则不必在您的文件中指定它pom.xml,因为它是测试的默认资源路径。

关于你的问题,你不应该使用new File("myfile");,而是应该使用MyTestClass.class.getResourceAsStream("/myfile");

  • Java 中的“File”类只是一个路径名。它可以引用文件**或**目录。所提到的“文件”甚至不需要存在。如果 `aFile = new File("Inbound")` 则 `aFile.isDirectory()` 将计算为 `true`。请参阅 http://docs.oracle.com/javase/6/docs/api/java/io/File.html 中“File”类的 Javadocs。 (3认同)