Loading Properties File In JUnit @BeforeClass

You need to add ${build.classes.dir} to compile.classpath.

Update: Based on communication in the comments, it turned out the classpath was not the problem. Instead the wrong class loader was used.

Class.getResourceAsStream() looks up the path of the resource based on the class loader the class was loaded by. As it turns out the Properties class was loaded by a different class loader than the Testing class, and the resource path was incorrect with relation to that class loader's classpath. The solution was to use Testing.class.getResourceAsStream(...) instead of Properties.class.getResourceAsStream(...).


This works:

YourTestClass.class.getClassLoader().getResourceAsStream

This doesn't work:

YourTestClass.class.getResourceAsStream

Tags:

Java

Junit

Ant