簡體   English   中英

在Maven多模塊項目的JUnit測試中找不到資源

[英]Unable to find resource in JUnit test in Maven multi-module project

我正在使用Maven 3.0.3,JUnit 4.8.1和Java 1.6。 我在JUnit測試文件中有這個...

@Before
public void loadTestProps() throws IOException, GeneralSecurityException { 
    final InputStream in = getClass().getClassLoader().getResourceAsStream("test.properties");
    testProps = new Properties();
    testProps.load(in);

未獲取“ test.properties”(輸入流對象為null)。 我的項目結構如下:

父+-pom.xml
+-戰爭模塊
+ ------ pom.xml
+ ------ src / test / java / JUnit文件
+ ------ src / test / resources / test.properties
+ ---耳模
+ ------- pom.xml

我寧願將“ src / test / resources”目錄保留在WAR模塊中,因為測試僅與WAR文件有關。 有什么辦法可以重寫上面的Java以找到我的“ test.properties”,還是必須將文件移出WAR模塊?

代替getResourceAsStream(“ test.properties”); 您應該使用getResourceAsStream(“ / test.properties”);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM