为什么我无法在使用 Maven 进行 Junit 测试运行时访问 src/test/resources?

发布于 2024-12-07 07:53:37 字数 172 浏览 0 评论 0原文

我在运行以下代码时遇到问题:

configService.setMainConfig("src/test/resources/MainConfig.xml");

来自 Junit @Before 方法。

这是 Maven 构建目标文件夹的方式吗?

I am having a problems running the following code:

configService.setMainConfig("src/test/resources/MainConfig.xml");

From within a Junit @Before method.

Is this the way Maven builds out its target folder?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

或十年 2024-12-14 07:53:37

直接访问MainConfig.xmlsrc/test/resources 目录内容放置在 CLASSPATH 的根目录中。

更准确地说:src/test/resources 的内容被复制到 target/test-classes 中,因此如果您有以下项目结构:

.
└── src
    └── test
        ├── java
        │   └── foo
        │       └── C.java
        └── resources
            ├── a.xml
            └── foo
                └── b.xml

它将产生以下测试 CLASSPATH内容:

  • /foo/C.class
  • /a.xml
  • /foo/b.xml

要实际从 Java 源访问文件,请使用
getClass().getResource("/MainConfig.xml").getFile()

Access MainConfig.xml directly. The src/test/resources directory contents are placed in the root of your CLASSPATH.

More precisely: contents of src/test/resources are copied into target/test-classes, so if you have the following project structure:

.
└── src
    └── test
        ├── java
        │   └── foo
        │       └── C.java
        └── resources
            ├── a.xml
            └── foo
                └── b.xml

It will result with the following test CLASSPATH contents:

  • /foo/C.class
  • /a.xml
  • /foo/b.xml

To actually access the files from Java source, use
getClass().getResource("/MainConfig.xml").getFile().

┾廆蒐ゝ 2024-12-14 07:53:37

我今天遇到了同样的问题,并且找到了一些解决方案。

首先,这是我的文件结构:

.
└── src
│   └── test
│       ├── java
│       │   └── mypackage
│       │       └── MyClassTest.java
│       └── resources
│           └── image.jpg
└── target
    └── test-classes
            ├── image.jpg
            └── mypackage
                └── MyClassTest.class  

什么不起作用:(Java 11 Syntax)

var imgFile = new File("image.jpg"); // I was expecting that Junit could find the file.
var absPath = file.getAbsolutePath(); // /home/<user>/../<project-root>/image.jpg
var anyFileUnderThisPath = file.exists(); // false

我们可以注意到,绝对路径根本没有指向我的图像!但如果我在项目根目录下有一个图像,那么它就会起作用。

解决方案1:路径(Java 7中引入)

var relPath = Paths.get("src", "test", "resources", "image.jpg"); // src/test/resources/image.jgp
var absPath = relPath.toFile().getAbsolutePath(); // /home/<user>/../<project-root>/src/test/resources/image.jpg
var anyFileUnderThisPath = new File(absPath).exists(); // true

正如我们所看到的,它指向正确的文件。

解决方案2:ClassLoader

var classLoader = getClass().getClassLoader();
var url = classLoader.getResource("image.jpg"); // file:/home/<user>/../<project-root>/target/test-classes/image.jpg
var file = new File(url.getFile()); // /home/<user>/../<project-root>/target/test-classes/image.jpg
var anyFileUnderThisPath = file.exists(); // true

注意,现在是在目标目录下搜索文件!它有效。

解决方案 3:文件(非工作示例的改编)

var absPath = new File("src/test/resources/image.jpg").getAbsolutePath();
var var anyFileUnderThisPath = new File(absPath).exists(); // true

在采用绝对路径并将 src/test/resources/ 作为前缀后也可以工作。

总结

所有三个解决方案都有效,但在我看来,必须放置 src/test/resources/ 并不优雅,这就是为什么我更喜欢第二个解决方案(<代码>类加载器)。

来源:

I ran into the same problem today and I have found some solutions.

First, here is my file structure:

.
└── src
│   └── test
│       ├── java
│       │   └── mypackage
│       │       └── MyClassTest.java
│       └── resources
│           └── image.jpg
└── target
    └── test-classes
            ├── image.jpg
            └── mypackage
                └── MyClassTest.class  

What is not working: (Java 11 synthax)

var imgFile = new File("image.jpg"); // I was expecting that Junit could find the file.
var absPath = file.getAbsolutePath(); // /home/<user>/../<project-root>/image.jpg
var anyFileUnderThisPath = file.exists(); // false

What we can notice is that the absolute path does not point at all on my image! But if I had an image under at the project-root, then it would have worked.

Solution 1: Paths (introduced in Java 7)

var relPath = Paths.get("src", "test", "resources", "image.jpg"); // src/test/resources/image.jgp
var absPath = relPath.toFile().getAbsolutePath(); // /home/<user>/../<project-root>/src/test/resources/image.jpg
var anyFileUnderThisPath = new File(absPath).exists(); // true

As we can see, it points on the right file.

Solution 2: ClassLoader

var classLoader = getClass().getClassLoader();
var url = classLoader.getResource("image.jpg"); // file:/home/<user>/../<project-root>/target/test-classes/image.jpg
var file = new File(url.getFile()); // /home/<user>/../<project-root>/target/test-classes/image.jpg
var anyFileUnderThisPath = file.exists(); // true

Note that now the file is searched under the target directory! and it works.

Solution 3: File (Adaptation of the non-working example)

var absPath = new File("src/test/resources/image.jpg").getAbsolutePath();
var var anyFileUnderThisPath = new File(absPath).exists(); // true

Which works also after taking the absolute path and putting src/test/resources/ as prefix.

Summary

All three solutions works but having to put src/test/resources/ is, in my own opinion not elegant, and this is why I would prefer the 2nd solution (ClassLoader).

Sources:

心病无药医 2024-12-14 07:53:37

我猜 setMainConfig 需要资源的路径,它将使用 ClassLoader 加载,而不是相对文件路径。如果您链接到这个神秘的 configService.setMainConfig 方法的 javadoc,将会有所帮助。

如果我的猜测是正确的,那么路径应该只是MainConfig.xml。 Mave 将 src/test/resources 的内容复制到 target/test-classes (IIRC) 文件夹。这个 test-classes 文件夹位于单元测试的类路径中。

I guess setMainConfig expects the path of a resource, that it will load using the ClassLoader, and not a relative file path. It would help if you linked to the javadoc of this mysterious configService.setMainConfig method.

If my guess is correct, then the path should just be MainConfig.xml. Mave copies the contents of src/test/resources to the target/test-classes (IIRC) folder. And this test-classes folder is in the classpath of the unit tests.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文