我知道我可以从src/test/resources加载一个文件:

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

但是我怎么能得到src/test/resources目录的完整路径,即我不想加载一个文件,我只想知道目录的路径?


当前回答

如果这是一个spring项目,我们可以使用下面的代码从src/test/resource文件夹中获取文件。

File file = ResourceUtils.getFile(this.getClass().getResource("/some_file.txt"));

其他回答

我将简单地使用Java 7中的Path

Path resourceDirectory = Paths.get("src","test","resources");

干净利落!

哇,正确答案还不在这里!

MyClass.class.getResource("/somefile");
MyClass.class.getResourceAsStream("/somefile");

https://javachannel.org/posts/how-to-access-static-resources/

你不需要干扰类加载器。事实上,这是一个不好的习惯,因为当类装入器资源在jar存档中时,它们不是java.io.File对象。

Maven在运行测试之前自动设置当前工作目录,因此您可以使用:

    File resourcesDirectory = new File("src/test/resources");

getabsolutepath()将返回正确的值,如果这是你真正需要的。

如果您希望您的测试通过文件系统访问数据,我建议创建一个src/test/data目录。这让你清楚地知道你在做什么。

src/test/resources中的所有内容都复制到target/test-classes文件夹中。所以在maven构建期间,要从测试资源中获取文件,你必须从test-classes文件夹中加载它,就像这样:

Paths.get(
    getClass().getProtectionDomain().getCodeSource().getLocation().toURI()
).resolve(
    Paths.get("somefile")
).toFile()

分解:

getClass(). getprotectiondomain (). getcodesource (). getlocation (). touri() -给你目标/测试类的URI。 resolve(Paths.get("somefile")) -将somefile解析到target/test-classes文件夹。

原来的答案是这样的

@Steve C和@ashosborne1提供的选项存在差异和限制。我想,它们必须具体说明。

File resourcesDirectory = new File("src/test/resources");?

1 When tests are going to be run via maven only but not via IDE. 2.1 When tests are going to be run via maven or 2.2 via IDE and only one project is imported into IDE. (I use “imported” term, cause it is used in IntelliJ IDEA. I think users of eclipse also import their maven project). This will work, cause working directory when you run tests via IDE is the same as your project. 3.1 When tests are going to be run via maven or 3.2 via IDE, and more than one projects are imported into IDE (when you are not a student, you usually import several projects), AND before you run tests via IDE, you manually configure working directory for your tests. That working directory should refer to your imported project that contains the tests. By default, working directory of all projects imported into IDE is only one. Probably it is a restriction of IntelliJ IDEA only, but I think all IDEs work like this. And this configuration that must be done manually, is not good at all. Working with several tests existing in different maven projects, but imported into one big “IDE” project, force us to remember this and don’t allow to relax and get pleasure from your work.

@ashosborne1提供的解决方案(我个人更喜欢这个)需要在运行测试之前完成2个额外的要求。下面是使用这个解决方案的步骤列表:

Create a test folder (“teva”) and file (“readme”) inside of “src/test/resources/”: src/test/resources/teva/readme File must be created in the test folder, otherwise, it will not work. Maven ignores empty folders. At least once build project via mvn clean install. It will run tests also. It may be enough to run only your test class/method via maven without building a whole project. As a result your test resources will be copied into test-classes, here is a path: target/test-classes/teva/readme After that, you can access the folder using code, already offered by @ashosborne1 (I'm sorry, that I could not edit this code inside of this list of items correctly):

TEVA_FOLDER = "teva";... URL tevaUrl = YourTest.class.getClassLoader().getResource(TEVA_FOLDER); 字符串tevaTestFolder = new File(tevaul . touri ()).getAbsolutePath();

现在您可以通过IDE运行您的测试,次数不限。直到你把mvn清理干净。它将删除目标文件夹。

在通过IDE运行测试之前,在测试文件夹中创建文件并首次运行maven是必需的步骤。如果没有这些步骤,如果您只是在IDE中创建测试资源,然后编写测试并仅通过IDE运行它,您将会得到一个错误。通过mvn运行测试会将测试资源复制到target/test-classes/teva/readme中,并且类加载器可以访问这些资源。

You may ask, why do I need import more than one maven project in IDE and why so many complicated things? For me, one of the main motivation: keeping IDA-related files far from code. I first create a new project in my IDE. It is a fake project, that is just a holder of IDE-related files. Then, I import already existing maven projects. I force these imported projects to keep IDEA files in my original fake project only. As a result I don't see IDE-related files among the code. SVN should not see them (don't offer to configure svn/git to ignore such files, please). Also it is just very convenient.