問題描述
我在運行以下代碼時遇到問題:
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?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!