PowerMockito and Final Methods

1,461 views
Skip to first unread message

sohr...@googlemail.com

unread,
Jun 16, 2009, 7:00:40 AM6/16/09
to PowerMock
I want to test a Class Using the java.net.URL final class. As this
class is final I can't do it with Mockito alone and stumbled upon
PowerMockito. I found some articles / changelogs that final classes
are supported in PowerMockito but no Tutorial on how to do it. My
class looks like this:

@RunWith(PowerMockRunner.class)
@PrepareForTest(DownloadThreadTest.class)

public class MyTestClass{
private URL url;

@Before public void setUp(){
url = PowerMockito.mock(URL.class);
}

@Test public void myTest(){
Mockito.when(url.openStream()).thenReturn(myStream);
}

Of course i left out some parts that i thought were not needed.

The error i get is following:
java.lang.NoClassDefFoundError: javassist/expr/ExprEditor
at
org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createNewClassloader
(AbstractTestSuiteChunkerImpl.java:172)
at
org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.chunkClass
(AbstractTestSuiteChunkerImpl.java:137)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>
(AbstractTestSuiteChunkerImpl.java:99)
at org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.<init>
(AbstractTestSuiteChunkerImpl.java:92)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>
(JUnit4TestSuiteChunkerImpl.java:57)
at
org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.<init>
(AbstractCommonPowerMockRunner.java:34)
at org.powermock.modules.junit4.PowerMockRunner.<init>
(PowerMockRunner.java:24)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native
Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance
(NativeConstructorAccessorImpl.java:39)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance
(DelegatingConstructorAccessorImpl.java:27)
at java.lang.reflect.Constructor.newInstance(Constructor.java:513)
at org.junit.internal.requests.ClassRequest.buildRunner
(ClassRequest.java:33)
at org.junit.internal.requests.ClassRequest.getRunner
(ClassRequest.java:28)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.<init>
(JUnit4TestReference.java:28)
at
org.eclipse.jdt.internal.junit4.runner.JUnit4TestClassReference.<init>
(JUnit4TestClassReference.java:24)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest
(JUnit4TestLoader.java:40)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests
(JUnit4TestLoader.java:30)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:445)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:673)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run
(RemoteTestRunner.java:386)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main
(RemoteTestRunner.java:196)
Caused by: java.lang.ClassNotFoundException: javassist.expr.ExprEditor
at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:252)
at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:320)
... 21 more


Thanks for your help

szczepiq

unread,
Jun 16, 2009, 8:37:51 AM6/16/09
to powe...@googlegroups.com
>java.lang.NoClassDefFoundError: javassist/expr/ExprEdito

you might be missing some PowerMock dependency on the classpath.

Szczepan

Johan Haleby

unread,
Jun 16, 2009, 9:09:28 AM6/16/09
to powe...@googlegroups.com
Just as Szczepan points out you're missing the Javassist dependency needed to use PowerMock so that's why you get that error message. You should download http://powermock.googlecode.com/files/powermock-1.2.5-with-dependencies.zip or use maven.

Unfortunatley you can't create a _mock object_ of the URL class with PowerMock right now because it's a final system class (you can read about the problems with final system classes here). Since PowerMock 1.2.5 you can mock _static_ methods of final system classes (using this approach) but you cannot mock "instance methods". I'm actually in the process of implementing support for this as well but I ran into some issues so I put on hold. But hopefully you'll be able to do it in the next version (which will also have much better Mockito support).

/Johan
Reply all
Reply to author
Forward
0 new messages