Why Can#39;t I access src/test/resources in Junit test run with Maven?(为什么我无法在使用 Maven 的 Junit 测试运行中访问 src/test/resources?)
问题描述
我在运行以下代码时遇到问题:
I am having a problems running the following code:
configService.setMainConfig("src/test/resources/MainConfig.xml");
从 Junit @Before 方法中.
From within a Junit @Before method.
这是 Maven 构建其目标文件夹的方式吗?
Is this the way Maven builds out its target folder?
推荐答案
直接访问MainConfig.xml
.src/test/resources
目录内容放置在您的 CLASSPATH 的根目录中.
Access MainConfig.xml
directly. The src/test/resources
directory contents are placed in the root of your CLASSPATH.
更准确地说:src/test/resources
的内容被复制到target/test-classes
中,所以如果你有以下项目结构:
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
它将产生以下测试 CLASSPATH 内容:
It will result with the following test CLASSPATH contents:
/foo/C.class
/a.xml
/foo/b.xml
要真正从 Java 源访问文件,请使用getClass().getResource("/MainConfig.xml").getFile()
.
To actually access the files from Java source, use
getClass().getResource("/MainConfig.xml").getFile()
.
这篇关于为什么我无法在使用 Maven 的 Junit 测试运行中访问 src/test/resources?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:为什么我无法在使用 Maven 的 Junit 测试运行中访问 src/test/resources?


基础教程推荐
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- 降序排序:Java Map 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01