Siddharth Mehta
Siddharth Mehta

Reputation: 465

mockito test gives no such method error when run as junit test but when jars are added manually in run confugurations, it runs well

I've been facing a peculiar problem. Basically, when I run my Mockito/PowerMockito test normally i.e. 'Run as Junit Test', it gives me the following error :

java.lang.NoSuchMethodError: org.mockito.mock.MockCreationSettings.isUsingConstructor()Z
at org.mockito.internal.creation.instance.InstantiatorProvider.getInstantiator(InstantiatorProvider.java:10)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:110)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:58)
at org.powermock.api.mockito.PowerMockito.mock(PowerMockito.java:203)
at org.powermock.api.extension.listener.AnnotationEnabler.standardInject(AnnotationEnabler.java:106)
at org.powermock.api.extension.listener.AnnotationEnabler.beforeTestMethod(AnnotationEnabler.java:54)
at org.powermock.tests.utils.impl.PowerMockTestNotifierImpl.notifyBeforeTestMethod(PowerMockTestNotifierImpl.java:90)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.executeTest(PowerMockJUnit44RunnerDelegateImpl.java:292)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTestInSuper(PowerMockJUnit47RunnerDelegateImpl.java:127)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit47RunnerDelegateImpl$PowerMockJUnit47MethodRunner.executeTest(PowerMockJUnit47RunnerDelegateImpl.java:82)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$PowerMockJUnit44MethodRunner.runBeforesThenTestThenAfters(PowerMockJUnit44RunnerDelegateImpl.java:282)
at org.junit.internal.runners.MethodRoadie.runTest(MethodRoadie.java:86)
at org.junit.internal.runners.MethodRoadie.run(MethodRoadie.java:49)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.invokeTestMethod(PowerMockJUnit44RunnerDelegateImpl.java:207)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.runMethods(PowerMockJUnit44RunnerDelegateImpl.java:146)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl$1.run(PowerMockJUnit44RunnerDelegateImpl.java:120)
at org.junit.internal.runners.ClassRoadie.runUnprotected(ClassRoadie.java:33)
at org.junit.internal.runners.ClassRoadie.runProtected(ClassRoadie.java:45)
at org.powermock.modules.junit4.internal.impl.PowerMockJUnit44RunnerDelegateImpl.run(PowerMockJUnit44RunnerDelegateImpl.java:122)
at org.powermock.modules.junit4.common.internal.impl.JUnit4TestSuiteChunkerImpl.run(JUnit4TestSuiteChunkerImpl.java:106)
at org.powermock.modules.junit4.common.internal.impl.AbstractCommonPowerMockRunner.run(AbstractCommonPowerMockRunner.java:53)
at org.powermock.modules.junit4.PowerMockRunner.run(PowerMockRunner.java:59)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

I had tried adding all the required jars in the maven dependency as well as in the build path but to no avail.

However, I then added the jars manually in run configurations->classpath->user entries. Then, I ran the test as I normally would and it displayed the green status bar i.e. it ran perfectly.

Please help me on this. I need to have the test run without these annoying changes which would affect my work. What could I do so that I cun run the test normally i.e. without going all the way to run configurations but simply by adding the jars in the build path and then 'run as junit test'?

Upvotes: 26

Views: 78265

Answers (6)

Eel Lee
Eel Lee

Reputation: 3543

The other answers gave me guidance, but what actually triggered the problem for me was that I accidentally imported

import static org.mockito.ArgumentMatchers.eq;

when the rest of the code uses

import static org.mockito.Matchers.any;

What's the problem, you ask?

Well, org.mockito.ArgumentMatchers was taken from the Mockito 2.23 and org.mockito.Matchers (and the rest of the mockito imports) was imported from the Mockito 1.9.

In the version 2.23 Matchers class extends ArgumentMatchers (so the static matchers are no longer there, they are in the ArgumentMatchers class) and is marked as @Deprecated

Anyway, that triggered java.lang.NoSuchMethodError message and the solution was to stick to the 1.9 version, and simply use

import static org.mockito.Matchers.eq;

Upvotes: 6

Manu Chadha
Manu Chadha

Reputation: 16747

In case it helps, I faced similar issue in IntelliJ. In principle, the problem is with incorrect mockito jar being picked. How it gets included and how to delete it could be project/IDE specific. My question and answer is here - An exception or error caused a run to abort: org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress()L

Upvotes: 0

Pravind Kumar
Pravind Kumar

Reputation: 911

I think there is an issue with Mockito version - I just updated the code from -

testCompile group: 'org.mockito', name: 'mockito-all', version: '1.8.5'

to

testCompile group: 'org.mockito', name: 'mockito-all', version: '1.10.19'

Upvotes: 3

Aniket Thakur
Aniket Thakur

Reputation: 68985

For me mockito version was creating issues. Just remove mockito dependency if you have added explicitly in your pom file. I just removed -

testCompile 'org.mockito:mockito-all:1.9.5' 

line from my gradle file. Lets power mock resolve and add mockito dependency it needs on it's own instead of you forcefully adding one which may led to conflict.

So for me dependencies are -

testCompile "org.powermock:powermock-module-junit4:1.6.4"
testCompile "org.powermock:powermock-module-junit4-rule:1.6.4" 
testCompile "org.powermock:powermock-api-mockito:1.6.4" 
testCompile "org.powermock:powermock-classloading-xstream:1.6.4" 

other than junit and other lib dependencies you may have.

Upvotes: 21

asg
asg

Reputation: 2456

Try Cntrl + Shift + T on your eclipse screen. Check what are your results. Ideally eclipse should resolve this class only from one dependency. If you are getting multiple results, you may need to add exclusion on certain dependencies.

enter image description here

Also check if you are able to execute your test cases from command prompt. If you are able to do it, then it's not a maven problem. But if something is failing here, check your dependencies.

Upvotes: 53

Nick Holt
Nick Holt

Reputation: 34321

Your IDE needs to be synchronized with Maven, which is what you're doing manually by adding the dependencies.

Unfortunately Eclipse doesn't (or didn't) provide this behavior out-of-the-box (unlike IntelliJ which synchronizes seamlessly).

The solution used to be done with the Maven Eclipse Plugin but looking at the docs it seems the latest way to do this is with Eclipse Maven Integration plugin for the IDE.

Upvotes: 1

Related Questions