I am having a problems running the following code:
我有一个运行以下代码的问题:
configService.setMainConfig("src/test/resources/MainConfig.xml");
From within a Junit @Before method.
从Junit @Before方法。
Is this the way Maven builds out its target folder?
这是Maven构建目标文件夹的方式吗?
2 个解决方案
#1
63
Access MainConfig.xml
directly. The src/test/resources
directory contents are placed in the root of your CLASSPATH.
访问MainConfig。直接xml。src/test/resources目录内容放在类路径的根目录中。
More precisely: contents of src/test/resources
are copied into target/test-classes
, so if you have the following project structure:
更准确地说:src/test/resources的内容被复制到目标/测试类中,所以如果您有以下项目结构:
.
└── src
└── test
├── java
│ └── foo
│ └── C.java
└── resources
├── a.xml
└── foo
└── b.xml
It will result with the following test CLASSPATH contents:
它将导致以下测试类路径内容:
/foo/C.class
- / foo / C.class
/a.xml
- / a.xml
/foo/b.xml
- / foo / b.xml
To actually access the files from Java source, use getClass().getResource("/MainConfig.xml").getFile()
.
要实际访问来自Java源代码的文件,请使用getClass(). getresource(“/MainConfig.xml”). getfile()。
#2
2
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.
我猜setMainConfig期望资源的路径,它将使用类加载器加载,而不是相对的文件路径。如果链接到这个神秘的configService的javadoc会有所帮助。setMainConfig方法。
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.
如果我的猜测是正确的,那么路径应该是MainConfig.xml。Mave将src/test/resources的内容复制到目标/测试类(IIRC)文件夹中。这个测试类文件夹位于单元测试的类路径中。
#1
63
Access MainConfig.xml
directly. The src/test/resources
directory contents are placed in the root of your CLASSPATH.
访问MainConfig。直接xml。src/test/resources目录内容放在类路径的根目录中。
More precisely: contents of src/test/resources
are copied into target/test-classes
, so if you have the following project structure:
更准确地说:src/test/resources的内容被复制到目标/测试类中,所以如果您有以下项目结构:
.
└── src
└── test
├── java
│ └── foo
│ └── C.java
└── resources
├── a.xml
└── foo
└── b.xml
It will result with the following test CLASSPATH contents:
它将导致以下测试类路径内容:
/foo/C.class
- / foo / C.class
/a.xml
- / a.xml
/foo/b.xml
- / foo / b.xml
To actually access the files from Java source, use getClass().getResource("/MainConfig.xml").getFile()
.
要实际访问来自Java源代码的文件,请使用getClass(). getresource(“/MainConfig.xml”). getfile()。
#2
2
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.
我猜setMainConfig期望资源的路径,它将使用类加载器加载,而不是相对的文件路径。如果链接到这个神秘的configService的javadoc会有所帮助。setMainConfig方法。
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.
如果我的猜测是正确的,那么路径应该是MainConfig.xml。Mave将src/test/resources的内容复制到目标/测试类(IIRC)文件夹中。这个测试类文件夹位于单元测试的类路径中。