为什么我无法在使用 Maven 进行 Junit 测试运行时访问 src/test/resources?
我在运行以下代码时遇到问题:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
直接访问
MainConfig.xml
。src/test/resources
目录内容放置在 CLASSPATH 的根目录中。更准确地说:
src/test/resources
的内容被复制到target/test-classes
中,因此如果您有以下项目结构:它将产生以下测试 CLASSPATH内容:
/foo/C.class
/a.xml
/foo/b.xml
要实际从 Java 源访问文件,请使用
getClass().getResource("/MainConfig.xml").getFile()
。Access
MainConfig.xml
directly. Thesrc/test/resources
directory contents are placed in the root of your CLASSPATH.More precisely: contents of
src/test/resources
are copied intotarget/test-classes
, so if you have the following project structure: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()
.我今天遇到了同样的问题,并且找到了一些解决方案。
首先,这是我的文件结构:
什么不起作用:(Java 11 Syntax)
我们可以注意到,绝对路径根本没有指向我的图像!但如果我在项目根目录下有一个图像,那么它就会起作用。
解决方案1:路径(Java 7中引入)
正如我们所看到的,它指向正确的文件。
解决方案2:ClassLoader
注意,现在是在目标目录下搜索文件!它有效。
解决方案 3:文件(非工作示例的改编)
在采用绝对路径并将
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:
What is not working: (Java 11 synthax)
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)
As we can see, it points on the right file.
Solution 2: ClassLoader
Note that now the file is searched under the target directory! and it works.
Solution 3: File (Adaptation of the non-working example)
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:
我猜
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 mysteriousconfigService.setMainConfig
method.If my guess is correct, then the path should just be MainConfig.xml. Mave copies the contents of
src/test/resources
to thetarget/test-classes
(IIRC) folder. And this test-classes folder is in the classpath of the unit tests.