Junit + getResourceAsStream Returning Null

2019-01-23 01:30发布

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?

Thanks!

11条回答
可以哭但决不认输i
2楼-- · 2019-01-23 01:43

From Java API:

http://docs.oracle.com/javase/6/docs/api/java/lang/ClassLoader.html#getSystemResource(java.lang.String)

Find a resource of the specified name from the search path used to load classes. This method locates the resource through the system class loader

So the syntax will for instance be: ClassLoader.getSystemResource("test.xml").toString();

Works like a charm!

查看更多
闹够了就滚
3楼-- · 2019-01-23 01:44

I had this problem with a Spring class. System.class.getResourceAsStream(...) worked in unit tests but not in Tomcat, Thread.currentThread().getContextClassLoader().getResourceAsStream(...) worked in Tomcat but not in unit tests.

Ulitimately I went with:

ClassUtils.getDefaultClassLoader()
    .getResourceAsStream("com/example/mail/templates/invoice-past-due.html"))

I also found that once I did it this way, I did not need to have the path starting with a slash.

查看更多
再贱就再见
4楼-- · 2019-01-23 01:45

I always have problem with this method. Here are 2 links, which might be useful:

I always experiment with adding "/" at the beginning or "./".

From my experience the best method is using FileInputStream. There is only one thing to remember (while using FileInputStream with Eclipse), with default settings, your working directory is set to projects root. You can always check where is your current directory (and what relative paths you need)using this piece of code.

查看更多
ら.Afraid
5楼-- · 2019-01-23 01:47

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.

查看更多
Anthone
6楼-- · 2019-01-23 01:53

I spent lot of time in this problem and it was missing me the following configuration: Right-click on an eclipse project and select Properties -> Java Build Path -> Source and edit Included row and add *.properties (*.fileExtension)

查看更多
神经病院院长
7楼-- · 2019-01-23 01:58

In case you are using Maven, add this part to your pom.xml

<build>
    <testResources>
        <testResource>
            <directory>${project.basedir}/src/test/resources</directory>
        </testResource>
    </testResources>
</build>

Your test.xml and other resource files must be located in src/test/resources

查看更多
登录 后发表回答