Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PowerMock throws NoSuchMethodError (setMockName)

Tags:

I'm trying to mock a constructor using PowerMockito but every time I run the test I get the following error:

java.lang.NoSuchMethodError: org.mockito.internal.creation.MockSettingsImpl.setMockName(Lorg/mockito/mock/MockName;)Lorg/mockito/internal/creation/settings/CreationSettings;
at org.powermock.api.mockito.internal.mockcreation.MockCreator.createMethodInvocationControl(MockCreator.java:107)
at org.powermock.api.mockito.internal.mockcreation.MockCreator.mock(MockCreator.java:60)
at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.createNewSubstituteMock(DefaultConstructorExpectationSetup.java:105)
at org.powermock.api.mockito.internal.expectation.DefaultConstructorExpectationSetup.withAnyArguments(DefaultConstructorExpectationSetup.java:71)

I have the following PowerMock dependencies in my project:

  • org.powermock:powermock-module-junit4:1.5.6
  • org.powermock:powermock-mockito-release-full:1.5.6

I've traced the dependency tree of my project and fixed conflicts so that mockito-all:1.9.5 gets included in the build.

like image 898
Psycho Punch Avatar asked Nov 25 '14 21:11

Psycho Punch


People also ask

Is PowerMock compatible with Mockito?

Supported versionsPowerMock version 2.0. 0 and upper has support of Mockito 2. PowerMock version 1.7. 0 and upper has experimental support of Mockito 2.

Why do we use PowerMockito?

PowerMockito is a PowerMock's extension API to support Mockito. It provides capabilities to work with the Java Reflection API in a simple way to overcome the problems of Mockito, such as the lack of ability to mock final, static or private methods.


2 Answers

Make sure powermockito and mockito versions are aligned as in this versions chart - MockitoUsage#supported-versions,

Mockito                     | PowerMock
------------------------------------------------------------------------------
2.0.0-beta - 2.0.42-beta    |   1.6.5+
------------------------------------------------------------------------------
1.10.19                     |   1.6.4
1.10.8 - 1.10.x             |   1.6.2+
1.9.5-rc1 - 1.9.5           |   1.5.0 - 1.5.6
1.9.0-rc1 & 1.9.0           |   1.4.10 - 1.4.12
1.8.5                       |   1.3.9 - 1.4.9
1.8.4                       |   1.3.7 & 1.3.8
1.8.3                       |   1.3.6
1.8.1 & 1.8.2               |   1.3.5
1.8                         |   1.3
1.7                         |   1.2.5

Easy way to find mockito and powermock-mockito version using maven is,

mvn dependency:tree | grep mockito
[INFO] |  \- org.mockito:mockito-core:jar:1.8.5:compile
[INFO] +- org.mockito:mockito-all:jar:1.8.5:compile
[INFO] +- org.powermock:powermock-api-mockito:jar:1.4.9:compile

Problem could be the conflicting versions of mockito in the application and the one that powermockito uses, conflicting as below in my case where I'm using powermock 1.6.5 which does not support mockito 1.8.5

mvn clean dependency:tree | grep mockito
[INFO] +- org.mockito:mockito-all:jar:1.8.5:compile

[INFO] \- org.powermock:powermock-api-mockito:jar:1.6.5:compile
[INFO]    +- org.mockito:mockito-core:jar:1.10.19:compile
[INFO]    \- org.powermock:powermock-api-mockito-common:jar:1.6.5:compile
like image 139
prayagupa Avatar answered Oct 10 '22 11:10

prayagupa


I had

org.mockito mockito-all 1.8.4

added to my pom.xml apart from powermock's dependecies, removing this worked for me.

like image 37
Abhijeet Avatar answered Oct 10 '22 10:10

Abhijeet