PowerMock throws NoSuchMethodError (setMockName)

I had

org.mockito mockito-all 1.8.4

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


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

My problem was due to conflicting versions of javassist in my project's (transitive) dependencies. What I did was search for all dependencies that put old version of javassist in the build, then exclude them. For example:

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-entitymanager</artifactId>
    <version>3.5.1-Final</version>
    <scope>provided</scope>
    <exclusions>
        <exclusion>
            <groupId>javassist</groupId>
            <artifactId>javassist</artifactId>
        </exclusion>
    </exclusions>
</dependency>

while migrating springboot from 1.5 to 2.0.7 The versions of mockito in springboot and powermock are different so explicitly give mockito dependency

This is compatible while migrating to springboot 2.0.7

testCompile "org.powermock:powermock-api-mockito2:${powermockVersion}"
testCompile group: 'org.mockito', name: 'mockito-core', version: '2.8.9'
testCompile "org.powermock:powermock-module-junit4:1.7.3"
testCompile "org.powermock:powermock-core:1.7.3"