Problem with getResourceAsStream not working when PowerMock is used

1,371 views
Skip to first unread message

Hege

unread,
Aug 27, 2009, 10:07:04 AM8/27/09
to PowerMock
Hi all,

First of all, thanks to the developers for a great tool for testing
Java ME code!

I run into a slight problem today with loading resources. I thought
I'd check out if it's something I'm doing wrong, or if this is already
a known issue.

It seems that trying to load resources from the classpath fails when
I'm using PowerMock. I always get null from getResourceAsStream(). I
have (dummy) test case to show the problem:

@RunWith(PowerMockRunner.class)
@PrepareForTest({Demo.class})
public class DemoTest {
@Test
public void testGetResource() {
InputStream is = getClass().getResourceAsStream("test.properties");
Assert.assertNotNull("Resource stream should be found", is);
}
}


The test will fail when the powermock annotations are in place, but
works if they are removed. Do you have any ideas what is causing this?
Probably something in the PowerMock class loader?

I did already figure out a workaround. I put the resource to another
package, and add @PowerMockIgnore to ignore that package. Then I can
use some dummy class in that package to load the resources. But I'd
rather have it working from the same package :)

Thanks,
Heikki

Johan Haleby

unread,
Aug 27, 2009, 11:42:01 AM8/27/09
to powe...@googlegroups.com
Hi,

First of all thanks for your encouragement. I think you've run into two different bugs in version 1.2.5 at the same time, issue 110 and issue 140. Issue 110 is fixed already and is available in trunk which should fix your problem. If you don't want to use the trunk version (you can check it out from subversion and do mvn install) you can, as a workaround, put the test in a different package structure as class under test and it should work as well I think.

/Johan

Johan Haleby

unread,
Aug 27, 2009, 11:44:21 AM8/27/09
to powe...@googlegroups.com
I think another workaround would be to renamed the class (DemoTest) to something not starting with Demo (i.e. not starting with the name of a class you're preparing for test).

/Johan

Hege

unread,
Aug 28, 2009, 2:34:00 AM8/28/09
to PowerMock
Hi and thanks for the reply!

I tried both workarounds you suggested, but didn't seem to help, at
least with 1.2.5. I changed the test class package and name to
"xemo.XemoTest", but I still get null from getResourceAsStream unless
I use the PowerMockIgnore annotation. I'll try to check out the latest
version from trunk and see if that works.

Heikki

Hege

unread,
Aug 28, 2009, 3:05:31 AM8/28/09
to PowerMock
Nope, doesn't seem like the trunk version works either. I still need
to use PowerMockIgnore to make the resource loading work.

Hege

unread,
Aug 28, 2009, 3:33:27 AM8/28/09
to PowerMock
Oh, and another (rather strange) note: When running the test using the
Eclipse JUnit plugin, it works (with both 1.2.5 and trunk versions).

I noted from the source that the powermock MockClassLoader doesn't
implement the findResource function. I'm not really familiar with how
classloaders work, but checking the javadocs state that "Class loader
implementations should override this method to specify where to find
resources."

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html#findResource(java.lang.String)

My guess on this is that the powermock classloader fails to find the
resource, the request is passed to the parent classloader. In eclipse,
the parent classloader contains the whole project in the classpath, so
it finds the resource. But when running from Ant, the parent
classloader knows nothing about the project classpath and therefore
can't find the resource.

Just guessing, I might be way off on this :)

Johan Haleby

unread,
Aug 28, 2009, 5:50:43 AM8/28/09
to powe...@googlegroups.com
Hmm that could be it actually. Very interesting observation and thanks for taking your time to test against trunk as well. I'll add this as an issue that we have to look into. Of course if you like you could try to fix it and send us a patch.

Thanks again!
/Johan

Hege

unread,
Aug 31, 2009, 4:03:35 AM8/31/09
to PowerMock
No problem, glad to help. But unfortunately I don't think I have the
time right now to try to work on a a fix.

-Heikki-


On Aug 28, 12:50 pm, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hmm that could be it actually. Very interesting observation and thanks for
> taking your time to test against trunk as well. I'll add this as an issue
> that we have to look into. Of course if you like you could try to fix it and
> send us a patch.
>
> Thanks again!
> /Johan
>
>
>
> On Fri, Aug 28, 2009 at 9:33 AM, Hege <heikki.vuolteen...@gmail.com> wrote:
>
> > Oh, and another (rather strange) note: When running the test using the
> > Eclipse JUnit plugin, it works (with both 1.2.5 and trunk versions).
>
> > I noted from the source that the powermock MockClassLoader doesn't
> > implement the findResource function. I'm not really familiar with how
> > classloaders work, but checking the javadocs state that "Class loader
> > implementations should override this method to specify where to find
> > resources."
>
> >http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html#fi...)<http://java.sun.com/j2se/1.5.0/docs/api/java/lang/ClassLoader.html#fi...>
Reply all
Reply to author
Forward
0 new messages