1

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?

Community
  • 1
  • 1
Withheld
  • 4,603
  • 10
  • 45
  • 76
  • 1
    I doubt that it works even in your actual application. – Bhesh Gurung May 08 '14 at 19:49
  • @BheshGurung Why? I can't argue with results actually read and used from that `myproj.properties`... Please explain. – Withheld May 08 '14 at 19:50
  • If you are really sure that it works then it must a custom classloader in case of the actual application. It's obvious that it's not the same classloader that you have when you run your tests. IMHO, I think it will be better for you to read up a bit on how the resources in classpath are read generally using class and classloader generally and use that instead because that would be portable. – Bhesh Gurung May 08 '14 at 20:00
  • @BheshGurung I agree. JUnit must be using something else but I am not familiar with what JUnit uses. I tried the original `InputStream is = ServiceUtils.class.getResourceAsStream("/myproj.properties");` as well, but still null result. – Withheld May 08 '14 at 20:09
  • 1
    That looks little better. Now you need to make sure that the folder, where that file is located, is added to the classpath while executing your tests. e.g. `WEB-INF/classes` inside the war is automatically added to the classpath by the server when the war is deployed. – Bhesh Gurung May 08 '14 at 20:11

1 Answers1

4

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).

John Farrelly
  • 7,289
  • 9
  • 42
  • 52
  • 2
    Yes, your assumption was correct, I am using Maven. And... for the life of me, I swear I tried your `"/myproj.properties"` (without success), but now, for some strange reason, it works! I most certainly understand now **why** it works (after printing the classpath at runtime), but why it didn't work before is beyond me. Thanks +1 and accepting. – Withheld May 08 '14 at 20:27
  • 1
    My guess is that you did `getResourceAsStream("myproj.properties")` (notice the missing leading `/` slash) - which doesn't work. I've made this mistake many times before myself! – John Farrelly May 08 '14 at 20:30