简体   繁体   中英

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

I'm using Maven 3.0.3, JUnit 4.8.1, and Java 1.6. I have this in a JUnit test file ...

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

"test.properties" is not getting picked up (the input stream object is null). My project structure is as follows:

parent + --- pom.xml
+ --- war module
+ ------ pom.xml
+ ------ src/test/java/JUnit file
+ ------ src/test/resources/test.properties
+ --- ear module
+ ------- pom.xml

I would prefer to keep my "src/test/resources" directory within my WAR module, since the test only pertains to the WAR file. Is there a way I can rewrite the above Java to find my "test.properties" or do I have to move the file out of the WAR module?

Instead of getResourceAsStream("test.properties"); you should use getResourceAsStream("/test.properties");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM