javagetresource

Junit + getResourceAsStream Returning Null


Not sure how this is possible. I re-read up on getResourceAsStream and it's always returning null.

InputStream source = this.getClass().getResourceAsStream("test.xml");

Right next to test.java in the Finder (using OS X and Eclipse) is test.xml

I can open it in TextWrangler and view it as existing with data inside.

This is a Junit test if it makes any difference. I went and looked at existing Junit tests on our system and I'm using it in the exactly same manner as a working example (as in where the file is located and the code itself).

What small difference could there be preventing I assume getClass() from returning the right path?


Solution

  • getResourceAsStream() is using the CLASSPATH, and as such it will load from wherever your classes are, not your source files.

    I suspect you need to copy your XML to the same directory as your .class file.