[英]junit test: configuration file with path variable to the resources
我有一個AttributesLoader類,它選擇一個配置文件,檢索變量並將其值設置為對象。
例:
if (validatePropertyValue(configuration.getString(PROPERTY_RECORD_LIMIT))) {
extractionAttributesBuilder.setRecordLimit(configuration.getInt(PROPERTY_RECORD_LIMIT));
}
代碼工作正常,但我很難為某些屬性編寫測試。 因為代碼拾取了一個文件,所以我的src / test / resources中有一個cfg文件。 屬性之一是目錄的路徑。
我嘗試這樣做:
path=new File(AttributesLoaderTest.class.getResource("/extractPath").getPath()));
但是它不起作用,因為它會尋找路徑:new File(AttributesLoaderTest.class.getResource(“ / extractPath”)。getPath())); 這沒用..
我的問題是:
如何在文件中提供到我的src / test / resources目錄的路徑,而無需對其進行嚴格設置?
path=/extractPath
一種選擇是將根作為系統屬性傳遞,並使用它來構成文件的路徑。
例如,在pom.xml
您可以按以下方式設置系統屬性:
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
<systemPropertyVariables>
<resourcesDirectory>${project.build.directory}/src/config/</resourcesDirectory>
[...]
</systemPropertyVariables>
</configuration>
</plugin>
在JUnit類中,您可以擁有:
path=new File(AttributesLoaderTest.class.getResource(System.getProperty("resourcesDirectory") + "/extractPath").getPath()));
注意:尚未測試上面的代碼,因此可能包含一些錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.