bobber205
bobber205

Reputation: 13372

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?

Upvotes: 59

Views: 58434

Answers (15)

humkins
humkins

Reputation: 10705

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

Upvotes: 29

codersaty
codersaty

Reputation: 43

I got a Similar issue. The problem in my case was that getResourceAsStream() was working fine for the main Application code but giving Null in case of unit-test. The problem was that for the main Application code it was looking in the target/classes folder for the file but for the unit-test it was looking in the target/test-classes. I fixed the problem by adding my file in the test/resources folder.

Upvotes: 0

one stevy boi
one stevy boi

Reputation: 576

Seems there are a lot of possible causes to this and mine was not found here. I figured out that my tests were no longer being detected/run by maven and this baeldung post also provided a lot of possible causes. My problem ended up being that at some point I altered the wrong pom and set my packaging to pom.

The test-compile step was being skipped completely and no new resources were ending up in the classpath. So, check your target/test-classes directory and make sure that your tests are actually compiling in the first place.

Upvotes: 0

Bandikatla Sai charan
Bandikatla Sai charan

Reputation: 109

try using classloader

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

Upvotes: 6

Stuart McIntyre
Stuart McIntyre

Reputation: 998

Put test.xml in the src/main/resources (or src/test/resources).

File file = ResourceUtils.getFile("classpath:test.xml");
String test = new String(Files.readAllBytes(file.toPath()));

Upvotes: 2

egroque
egroque

Reputation: 67

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)

Upvotes: 3

Yuliia Ashomok
Yuliia Ashomok

Reputation: 8598

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

enter image description here

Then call next from your jUnit test:

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

Upvotes: -1

Kip
Kip

Reputation: 109493

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.

Upvotes: -2

Espen
Espen

Reputation: 49

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!

Upvotes: 0

Benedikt
Benedikt

Reputation: 1

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.

Upvotes: -1

surajz
surajz

Reputation: 3611

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

Upvotes: 36

limc
limc

Reputation: 40176

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

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

Upvotes: 13

Sebastian Łaskawiec
Sebastian Łaskawiec

Reputation: 2737

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.

Upvotes: 13

Paul Croarkin
Paul Croarkin

Reputation: 14675

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

Upvotes: 0

Brian Agnew
Brian Agnew

Reputation: 272397

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.

Upvotes: 27

Related Questions