Junit + getResourceAsStream Returning Null

一世执手 提交于 2019-11-28 22:31:01

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.

It's not finding the resource on the classpath. If you are using junit and maven make sure the resources are copied on the target/test-classes by adding <include> file directive on <testResource> section

You can also find out the location of your class in the file system by using

this.getClass().getResource(".")

and checking to see if the resource is there

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.

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

Assuming test.xml is located right under your test root source folder, do this:-

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

Try MyClass.getResourceAsStream().

Also try putting the test.xml in your classpath. For instance, in an Eclipse web project put text.xml in webcontent/WEB-INF/classes

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!

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)

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.

You need to put the copy of your resource file to the test resources, in my example it is font file:

Then call next from your jUnit test:

 public static InputStream getFontAsStream() throws IOException {
        return Thread.currentThread().getContextClassLoader()
                    .getResourceAsStream("fonts/" + "FreeSans.ttf");
    }

Add the folder that your having your resource files in to the source folders of eclipse. Then the file should be automatically put in the bin directory.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!