繁体   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