10

I'm trying to load sample.properties from my classpath during my JUnit test execution and it can't find the file in the class path. If I write a Java Main class I'm able to load the file just fine. I'm using the below ant task to execute my JUnit.

public class Testing {
 @BeforeClass
    public static void setUpBeforeClass() throws Exception {   
        Properties props = new Properties();
        InputStream fileIn = props_.getClass().getResourceAsStream("/sample.properties");
        **props.load(fileIn);**
    }

}

JUnit:

<path id="compile.classpath">
        <pathelement location="${build.classes.dir}"/>
    </path>
    <target name="test" depends="compile">
            <junit haltonfailure="true">
                <classpath refid="compile.classpath"/>
                <formatter type="plain" usefile="false"/>
                <test name="${test.suite}"/>
            </junit>
        </target>
        <target name="compile">
            <javac srcdir="${src.dir}" 
                   includeantruntime="false"
                   destdir="${build.classes.dir}" debug="true" debuglevel="lines,vars,source">
                <classpath refid="compile.classpath"/>
            </javac>
            <copy todir="${build.classes.dir}">
                <fileset dir="${src.dir}/resources"
                         includes="**/*.sql,**/*.properties" />
            </copy>
        </target>

Output:

[junit] Tests run: 0, Failures: 0, Errors: 1, Time elapsed: 0.104 sec
[junit] 
[junit] Testcase: com.example.tests.Testing took 0 sec
[junit]     Caused an ERROR
[junit] null
[junit] java.lang.NullPointerException
[junit]     at java.util.Properties$LineReader.readLine(Properties.java:418)
[junit]     at java.util.Properties.load0(Properties.java:337)
[junit]     at java.util.Properties.load(Properties.java:325)
[junit]     at com.example.tests.Testing.setUpBeforeClass(Testing.java:48)
[junit]
c12
  • 9,557
  • 48
  • 157
  • 253

2 Answers2

10

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(...).

Marek J
  • 1,364
  • 8
  • 18
  • 33
Attila
  • 28,265
  • 3
  • 46
  • 55
  • thanks for the response. I added the compile.classpath that included the ${build.classes.dir} which resolves to build/classes dir above since it was already like what you suggested, so that isn't my issue unfortunately. – c12 Apr 02 '12 at 21:42
  • The only other time this can happen (AFAIK) is when you are trying to load the resource from a class that was loaded by a different class-loader than your own class. Try `getClass().getReasourceAsStream(...)` instead of `prop.getClass().getResourceAsStream(...)`. Let me know if this fixes your problem and I'll update the answer – Attila Apr 03 '12 at 00:27
  • 2
    InputStream is = Testing.class.getClassLoader().getResourceAsStream("sample.properties"); worked, thanks for the suggestion. – c12 Apr 03 '12 at 00:44
  • I have updated my answer. Please accept it as it now contains the solution to your problem. – Attila Apr 03 '12 at 01:06
3

This works:

YourTestClass.class.getClassLoader().getResourceAsStream

This doesn't work:

YourTestClass.class.getResourceAsStream

ACV
  • 9,964
  • 5
  • 76
  • 81