Closed GoogleCodeExporter closed 8 years ago
From Hege:
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 :)
Original comment by johan.ha...@gmail.com
on 28 Aug 2009 at 9:51
Geoff Hartnell has made it work with this solution:
/* The search path for classes and resources */
private URLClassPath ucp;
// and in the constructor
// initialise ucp for findResource
ucp = sun.misc.Launcher.getBootstrapClassPath();
/**
* Finds the resource with the specified name on the search path.
*
* @param name the name of the resource
* @return a <code>URL</code> for the resource, or <code>null</
code>
* if the resource could not be found.
*/
public URL findResource(final String name) {
URL url = getClass().getClassLoader().getResource(name);
if (url != null && ucp != null)
{
// System.out.println("MockCLassLoader: URL is " +
// (url != null ? "valid " : "null") + " for resource "
+ name);
return ucp.checkURL(url);
}
return null;
}
Original comment by johan.ha...@gmail.com
on 18 Nov 2009 at 9:36
Since the solution above is using non-standard classes an attempt has been made
to
just invoke the findResource of the deferred classloader.
Original comment by johan.ha...@gmail.com
on 8 Dec 2009 at 10:16
This fixed the problem
Original comment by johan.ha...@gmail.com
on 9 Dec 2009 at 10:22
I have the same problem (resources from a jar aren't read)in the last version
of powermock (1.4.10) ... Do I have to use the same workaround????
thanks
Original comment by amanriqu...@gmail.com
on 18 Oct 2011 at 11:06
As user avobe says, I'm having same issue. But the workaround is easier:
in 'junit' task, use the parameter 'fork="true"', and won't be used the ANT
JavaVM.
Original comment by useyour....@gmail.com
on 20 Jul 2012 at 10:33
I am also having this issue in an eclipse environment.
Unable to resolve a resource whereas when not using the the powermock runnr it
works fine.
Original comment by sgandon
on 7 Dec 2012 at 12:29
I found a way around this issue in an OSGI environment.
the idea is to get an OSGI classloader to find the resources.
so for a class that is being tested with the PowerMockRunner you can get the
initial classloader
getClass().getClassLoader().getClass().getClassLoader()
so if you you want a stream on a resource you can do this.
getClass().getClassLoader().getClass().getClassLoader()
.getResourceAsStream("/bundle/path/to/resource");
or to make it a little smaller
getClass().getClassLoader().getClass()
.getResourceAsStream("/bundle/path/to/resource");
I also found that many OSGI APIs such as FrameworkUtil.getBundle (Class<T>
clazz) do not work in classes loaded with PowerMock.
I really hope Power mock get's OSGIfied some time soon.
Original comment by sgandon
on 10 Dec 2012 at 1:58
Unfortunately this will probably not happen any time soon unless someone helps
out with it :( You're very welcome to do so if you want.
Original comment by johan.ha...@gmail.com
on 10 Dec 2012 at 2:32
Original issue reported on code.google.com by
johan.ha...@gmail.com
on 28 Aug 2009 at 9:50