Get the Path of the /src/test/resources Directory in JUnit – 获取JUnit中/src/test/resources目录的路径

最后修改: 2019年 5月 7日

中文/混合/英文(键盘快捷键:t)

1. Overview

1.概述

Sometimes during unit testing, we might need to read a file from the classpath or pass a file to an object under test. We might also have a file in src/test/resources with data for stubs that could be used by libraries like WireMock.

在单元测试期间,有时我们可能需要从classpath中读取一个文件,或者将一个文件传递给被测对象。我们也可能在src/test/resources中拥有一个文件,其中包含可被WireMock等库使用的存根数据。

In this tutorial, we’ll learn how to read the path of the /src/test/resources directory.

在本教程中,我们将学习如何读取/src/test/resources目录的路径

2. Maven Dependencies

2.Maven的依赖性

First, we’ll need to add JUnit 5 to our Maven dependencies:

首先,我们需要将JUnit 5添加到我们的Maven依赖项中。

<dependency>
    <groupId>org.junit.jupiter</groupId>
    <artifactId>junit-jupiter-engine</artifactId>
    <version>5.8.1</version>
</dependency>

We can find the latest version of JUnit 5 on Maven Central.

我们可以在Maven Central上找到JUnit 5的最新版本。

2. Using java.io.File

2.使用java.io.File

The simplest approach uses an instance of the java.io.File class to read the /src/test/resources directory by calling the getAbsolutePath() method:

最简单的方法是使用java.io.File类的一个实例,通过调用getAbsolutePath()方法来读取/src/test/resources 目录。

String path = "src/test/resources";

File file = new File(path);
String absolutePath = file.getAbsolutePath();

System.out.println(absolutePath);

assertTrue(absolutePath.endsWith("src/test/resources"));

Note that this path is relative to the current working directory, meaning the project directory.

注意,该路径是相对于当前工作目录的,即项目目录。

Let’s look at an example output when running the test on macOS:

让我们看一下在macOS上运行测试时的输出示例。

/Users/user.name/my_projects/tutorials/testing-modules/junit-5-configuration/src/test/resources

3. Using Path

3.使用Path

Next, we can use the Path class, which was introduced in Java 7.

接下来,我们可以使用Path类,它是在Java 7中引入的

First, we need to call a static factory method, Paths.get(). Then we’ll convert Path to File. Finally, we just need to call getAbsolutePath(), as in the previous example:

首先,我们需要调用一个静态工厂方法,Paths.get().然后我们将Path转换为File.最后,我们只需要调用getAbsolutePath(),就像在前面的例子中。

Path resourceDirectory = Paths.get("src","test","resources");
String absolutePath = resourceDirectory.toFile().getAbsolutePath();

System.out.println(absolutePath);
 
Assert.assertTrue(absolutePath.endsWith("src/test/resources"));

And we get the same output as in the previous example too:

而我们得到的输出结果也和前面的例子一样。

/Users/user.name/my_projects/tutorials/testing-modules/junit-5-configuration/src/test/resources

4. Using ClassLoader

4.使用ClassLoader

Lastly, we can also use a ClassLoader:

最后,我们还可以使用一个ClassLoader

String resourceName = "example_resource.txt";

ClassLoader classLoader = getClass().getClassLoader();
File file = new File(classLoader.getResource(resourceName).getFile());
String absolutePath = file.getAbsolutePath();

System.out.println(absolutePath);

assertTrue(absolutePath.endsWith("/example_resource.txt"));

Let’s have a look at the output:

让我们看一下输出结果。

/Users/user.name/my_projects/tutorials/testing-modules/junit-5-configuration/target/test-classes/example_resource.txt

Note that this time, we have a /junit-5-configuration/target/test-classes/example-resource.txt file. It differs when we compare the result to the previous methods.

注意,这次我们有一个/junit-5-configuration/target/test-classes/example-resource.txt文件。当我们将结果与之前的方法进行比较时,它是不同的。

This is because the ClassLoader looks for the resources on the classpath. In Maven, the compiled classes and resources are put in the /target/ directory. That’s why this time, we got a path to a classpath resource.

这是因为ClassLoader会在classpath上寻找资源。在Maven中,编译后的类和资源都放在/target/目录下。这就是为什么这次我们得到了一个指向classpath资源的路径。

5. Conclusion

5.总结

In this brief article, we discussed how to read a /src/test/resources directory in JUnit 5.

在这篇简短的文章中,我们讨论了如何在JUnit 5中读取/src/test/resources目录。

Depending on our needs, we can achieve our goal with multiple methods: File, Paths, or ClassLoader classes.

根据我们的需要,我们可以通过多种方法实现我们的目标。文件路径、或ClassLoader类。

As always, all of the examples are available over on our GitHub project!

一如既往,所有的例子都可以在我们的GitHub项目上找到。