Cannot start with PowerMock, NoSuchMethodError...

4,807 views
Skip to first unread message

jawpio

unread,
Jul 8, 2009, 4:47:18 AM7/8/09
to PowerMock
Hi All,
I'm trying to use PowerMock but with no luck. The code I wrote is
straight forward...

import junit.framework.Assert;
import junit.framework.TestCase;

import org.easymock.EasyMock;
import org.junit.runner.RunWith;
import org.powermock.api.easymock.PowerMock;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PrepareForTest(AaaStaticClass.class)
public class PowerMockTest extends TestCase {

public void testStatic() throws Exception {
PowerMock.mockStatic(AaaStaticClass.class);
EasyMock.expect(AaaStaticClass.getAaa()).andReturn("Bbb");
Assert.assertEquals("Bbb", AaaStaticClass.getAaa());
}

}


Unfortunately I'm stuck with following error:

java.lang.NoSuchMethodError:
org.junit.internal.runners.MethodValidator.<init>(Lorg/junit/internal/
runners/TestClass;)V
at
org.powermock.modules.junit4.internal.impl.testcaseworkaround.PowerMockJUnit4MethodValidator.<init>
(PowerMockJUnit4MethodValidator.java:43)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.validate
(PowerMockJUnit44RunnerDelegateImpl.java:118)
at
org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.<init>
(PowerMockJUnit44RunnerDelegateImpl.java:82)
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:494)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader
(JUnit4TestSuiteChunkerImpl.java:149)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.createDelegatorFromClassloader
(JUnit4TestSuiteChunkerImpl.java:49)
at
org.powermock.tests.utils.impl.AbstractTestSuiteChunkerImpl.createTestDelegators
(AbstractTestSuiteChunkerImpl.java:183)
at
org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.<init>
(JUnit4TestSuiteChunkerImpl.java:69)
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:494)
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)


Thanks for any help...
Cheers,
Piotr

Johan Haleby

unread,
Jul 8, 2009, 6:30:30 AM7/8/09
to powe...@googlegroups.com
You're probably missing some dependency, download powermock-1.2.5-with-dependencies.zip and make sure all dependencies is in the class path and then try again.

/Johan

jawpio

unread,
Jul 8, 2009, 7:11:48 AM7/8/09
to PowerMock
Thanks Johan,

Works like a charm, sorry for the problem..

On 8 Lip, 12:30, Johan Haleby <johan.hal...@gmail.com> wrote:
> You're probably missing some dependency, download
> powermock-1.2.5-with-dependencies.zip<http://powermock.googlecode.com/files/powermock-1.2.5-with-dependenci...>and
> make sure all dependencies is in the class path and then try again.
>
> /Johan
>

Johan Haleby

unread,
Jul 8, 2009, 8:22:19 AM7/8/09
to powe...@googlegroups.com
No problem, glad that you got it working. Good luck!

gtyagi

unread,
Aug 7, 2009, 12:52:37 AM8/7/09
to PowerMock
Hi

I get the same error. I had downloaded the powermock-1.2.5-with-
dependencies.zip and put all the jars in my class path, but it didnt
help. :(

Thanks
GT

On Jul 8, 7:22 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> No problem, glad that you got it working. Good luck!
>

gokul a

unread,
Aug 7, 2009, 1:37:17 AM8/7/09
to powe...@googlegroups.com
Hi,

Remove the "extends TestCase" from your test class.

Put "@Test" annotation in each of the test cases you have written and try.

Regards,
Gopal
--
Regards,
Gopal

Johan Haleby

unread,
Aug 7, 2009, 2:10:19 AM8/7/09
to powe...@googlegroups.com
Does that help? In that case I suspect that it can be a bug in our JUnit runner when you extend TestCase.

gtyagi

unread,
Aug 7, 2009, 10:12:25 AM8/7/09
to PowerMock
Hi

Thanks for the reply. Here's what im trying to do:
I need to test a controller in which i need to instantiate a class and
use one of its methods.
Controller:
UserCredentials userCredentials = WebUserCredentialsUtils.from
(request);
StatusBean StatusBean = new StatusBean
(userCredentials.getSystem());
Collection<Group> Groups = statusBean.getgroupsAsMap().values
();
......

So i need to mock the instantiation of StatusBean class (it has no
corresponding interface). i tried doing sth. like :
statusBean = createMock(StatusBean.class, "SomeRegion");
expectNew(StatusBean.class, "SomeRegion").andReturn(statusBean);
expect(statusBean.getGroupsAsMap()).andReturn(groupMap);
replay(statusBean, StatusBean.class);
.....

I have all the jar files from the powermock zip with all dependencies.
But it still gives me an error at line :
statusBean = createMock(StatusBean.class, "SomeRegion");

java.lang.NoSuchMethodError:
org.easymock.classextension.internal.MocksClassControl.createMock
(Ljava/lang/Class;Lorg/easymock/classextension/ConstructorArgs;[Ljava/
lang/reflect/Method;)Ljava/lang/Object;
at org.powermock.api.easymock.PowerMock.doCreateMock(PowerMock.java:
2170)
at org.powermock.api.easymock.PowerMock.doMock(PowerMock.java:2140)
at org.powermock.api.easymock.PowerMock.createMock(PowerMock.java:
132)
at
com.ubs.swiskey.web.console.DashboardDisplayControllerMockitoTests.setup
(DashboardDisplayControllerMockitoTests.java:62)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke
(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke
(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.junit.internal.runners.MethodRoadie.runBefores
(MethodRoadie.java:122)
at
org.junit.internal.runners.MethodRoadie.runBeforesThenTestThenAfters
(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:
77)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:42)
at org.junit.internal.runners.JUnit4ClassRunner.invokeTestMethod
(JUnit4ClassRunner.java:88)
at org.junit.internal.runners.JUnit4ClassRunner.runMethods
(JUnit4ClassRunner.java:51)
at org.junit.internal.runners.JUnit4ClassRunner$1.run
(JUnit4ClassRunner.java:44)
at org.junit.internal.runners.ClassRoadie.runUnprotected
(ClassRoadie.java:27)
at org.junit.internal.runners.ClassRoadie.runProtected
(ClassRoadie.java:37)
at org.junit.internal.runners.JUnit4ClassRunner.run
(JUnit4ClassRunner.java:42)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run
(JUnit4TestReference.java:38)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run
(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests
(RemoteTestRunner.java:460)
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)

I have tried mocking a new instance using Easymock/mockito but didnt
have much luck there. I was following this example for my requirement:
http://code.google.com/p/powermock/wiki/MockConstructor

Thanks
GT




On Aug 7, 1:10 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Does that help? In that case I suspect that it can be a bug in our JUnit
> runner when you extend TestCase.
>
> On Fri, Aug 7, 2009 at 7:37 AM, gokul a <gokul0...@gmail.com> wrote:
> > Hi,
>
> > Remove the "extends TestCase" from your test class.
>
> > Put "@Test" annotation in each of the test cases you have written and try.
>
> > Regards,
> > Gopal
>

gtyagi

unread,
Aug 7, 2009, 10:13:23 AM8/7/09
to PowerMock
Also regarding using "@Test" - I am using "@Before" and "@Test".

On Aug 7, 1:10 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Does that help? In that case I suspect that it can be a bug in our JUnit
> runner when you extend TestCase.
>
> On Fri, Aug 7, 2009 at 7:37 AM, gokul a <gokul0...@gmail.com> wrote:
> > Hi,
>
> > Remove the "extends TestCase" from your test class.
>
> > Put "@Test" annotation in each of the test cases you have written and try.
>
> > Regards,
> > Gopal
>

Johan Haleby

unread,
Aug 7, 2009, 10:52:55 AM8/7/09
to powe...@googlegroups.com
Hmm, what is "SomeRegion"? Is that the constructor argument that is returned from the userCredentials.getSystem()? In that case I suggest that you do:

StatusBean statusBean = createMockAndExpectNew(StatusBean.class, "SomeRegion");
expect(statusBean.getGroupsAsMap()).andReturn(groupMap);
replayAll();
// invoke test
verifyAll();

That ought to work if you prepare the Controller for test using @PrepareForTest(Controller.class) and you must use the PowerMock JUnit runner using @RunWith(PowerMockRunner.class) at the class-level of your test.

Another solution would be to mock the static call to WebUserCredentialsUtils.from(..).

/Johan

gtyagi

unread,
Aug 7, 2009, 12:22:10 PM8/7/09
to PowerMock
Hi Johan

Thanks for the suggestion but unfortunately im still getting that
error. Here is the complete test I am using:

@RunWith(PowerMockRunner.class)
@PrepareForTest(ControllerToBeTested.class)
public class ControllerToBeTestedTestPM {
....
...
@Before
public void setup() {
request = new MockHttpServletRequest("GET", "/SomeView.do");
userCredentials = PowerMock.createPartialMock
(UserCredentials.class, "getSystem");
userCredentials.setValid(true);

expect(userCredentials.getSystem()).andReturn("SomeRegion");
replay(userCredentials);

WebContext context = new WebContext();
context.put("UserCredentials", userCredentials);
request.getSession(true)
.setAttribute( "...", context);

try {
statusBean = createMockAndExpectNew(StatusBean.class,
"SomeRegion");
} catch (Exception e) {
e.printStackTrace();
}

expect(statusBean.getGroupsAsMap()).andReturn(groupMap);
replay(statusBean);
...
...
...
}

@Test
public void testRetrieveStatuses() {
controllerToBeTested= new ControllerToBeTested();
ModelAndView mav = controllerToBeTested.retrieveStatuses
(request);
....
...
}

It gives an error "NoSuchMethosError" for line :
statusBean = createMockAndExpectNew(StatusBean.class, "SomeRegion");

Thanks
GT






On Aug 7, 9:52 am, Johan Haleby <johan.hal...@gmail.com> wrote:
> Hmm, what is "SomeRegion"? Is that the constructor argument that is returned
> from the userCredentials.getSystem()? In that case I suggest that you do:
>
> StatusBean statusBean = createMockAndExpectNew(StatusBean.class,
> "SomeRegion");
> expect(statusBean.getGroupsAsMap()).andReturn(groupMap);
> replayAll();
> // invoke test
> verifyAll();
>
> That ought to work if you prepare the Controller for test using
> @PrepareForTest(Controller.class) and you must use the PowerMock JUnit
> runner using @RunWith(PowerMockRunner.class) at the class-level of your
> test.
>
> Another solution would be to mock the static call to
> WebUserCredentialsUtils.from(..).
>
> /Johan
>

Johan Haleby

unread,
Aug 7, 2009, 3:20:17 PM8/7/09
to powe...@googlegroups.com
Hmm, how does the constructor of the StatusBean.class look like?

/Johan

gtyagi

unread,
Aug 9, 2009, 10:35:42 AM8/9/09
to PowerMock
HI Johan

The class StatusBean does not have a default (no-arg) constructor. It
has 2 other overloaded constructors :

public StatusBean(Context ctx) {
this.context = ctx;
UserCredentials user = UserCredentials.from(context);
system = user.getSystem();
}
and

public StatusBean(String system) {
this.system = system;
}

In the controller under test we are using the second one to
instantiate the class.

Thanks
GT
> ...
>
> read more »

gtyagi

unread,
Aug 9, 2009, 11:59:51 AM8/9/09
to PowerMock
Hi

I observed that I am getting this error with all of the powermock
methods, be it be createStrictMock(), createPartialMock() or
createMockAndExpectNew(). Is it that I am missing any jar or not using
the right version of the API. The jars I downloaded and using :

cglib-nodep-2.1_3.jar
easymock-2.4.jar
easymockclassextension-2.4.jar
javassist-3.9.0.GA.jar
junit-4.4.jar
mockito-all-1.7.jar
objenesis-1.1.jar
powermock-api-easymock-1.2.5.jar
powermock-api-mockito-1.2.5.jar
powermock-core-1.2.5.jar
powermock-module-junit3-1.2.5.jar
powermock-module-junit4-1.2.5.jar
powermock-module-junit4-common-1.2.5.jar
powermock-module-junit4-legacy-1.2.5.jar
powermock-reflect-1.2.5.jar

I also have some existing versions of easymock(2.2) in my projects
classpath. would having multiple version be a prob?

Thanks GT

gtyagi

unread,
Aug 10, 2009, 8:36:35 PM8/10/09
to PowerMock
Hi

Johan i was able to resolve the issue. Not sure what resolved it but
had tried a couple of things including having only one version of jars
in the classpath.
Thanks for all your help and the FAQs were pretty helpful too.

Thanks
GT
> ...
>
> read more »

Johan Haleby

unread,
Aug 11, 2009, 2:15:32 AM8/11/09
to powe...@googlegroups.com
Glad to hear that you got it working. Good luck!

/Johan
Reply all
Reply to author
Forward
0 new messages