Frage

This same statement works just fine in my actual application (web service):

InputStream is = ServiceUtils.class.getResourceAsStream(
  "file:/C:/Users/withheld/workspace/myproj/src/main/webapp/WEB-INF/classes/myproj.properties");

But inside a JUnit test module, it just keeps returning null.

Why?

At first, I thought this was a classpath issue, so I added a path as described here.

But that didn't help.

So, I forced a brute absolute path, using the method described in this other SO thread.

But it still returning null.

What am I doing wrong?

War es hilfreich?

Lösung

I assume you're using maven (based on the paths in your project). The classes directory under WEB-INF should be on the classpath when your web application is loaded, so try the following:

InputStream is = ServiceUtils.class.getResourceAsStream("/myproj.properties");

Note that this assumes you have myproj.properties in either your src/main/resources or src/test/resources directory (it will automatically get copied to WEB-INF/classes when maven builds the war or runs the tests).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top