21

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.

4
  • Can you conform to this setup and see if it fixes the issue
    – user180100
    Commented Nov 25, 2014 at 21:21
  • That's what I used originally but it generates the same error. Commented Nov 26, 2014 at 13:50
  • Perhaps you should add you complete dependency tree
    – user180100
    Commented Nov 26, 2014 at 16:01
  • It might have been something to do with my IDE. I rebuilt the project and the problem is gone... but replaced with another. Commented Nov 26, 2014 at 16:29

7 Answers 7

55

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
8

I had

org.mockito mockito-all 1.8.4

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

3
  • 2
    Thanks, that fixed it for me - basically if you are using Powermock you should let Powermock define the dependencies for Mockito, rather than overriding them with a potentially out of date version
    – PaulNUK
    Commented Oct 5, 2016 at 9:00
  • 1
    This was the correct answer. Having Powermock and mockito-all conflicted. Commented Dec 13, 2016 at 15:01
  • 1
    Worked for me too, Don't explicitly add mockito. Let powermock download and resolve the dependency based on it's version. Commented Apr 9, 2017 at 14:54
7

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>
2
  • how can I find - which dependencies are using javaassist old version ? BDW I am using gradle ... please guide Commented May 6, 2017 at 6:30
  • You can use :dependencies task in Gradle to see which ones use javassist. You can use utilities like grep, less etc. to help search. When you find which ones use it, exclude them and repeat the process until you have excluded all javassist (transitive) dependencies. Then, either work backwards until you have the version of javassist you need from another dependency, or add the version you want as dependency explicitly. That's how I'd do it at least. Commented May 6, 2017 at 6:43
1

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"
0

For me, in Eclipse, the fix to this problem was found in Java Build Path. Click on Order and Export tab. Move Web App Libraries to bottom. Note, that when appropriate, this will also allow you to view source of 3rd party libraries when Eclipse tells you that source cannot be found.

0

In my case it was a conflict dependency. I had fix it after exclude mockito-core artifact:

 <dependency>
        <groupId>com.googlecode.catch-exception</groupId>
        <artifactId>catch-exception</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.mockito</groupId>
                <artifactId>mockito-core</artifactId>
            </exclusion>
        </exclusions>
        <version>1.0.4</version>
        <scope>test</scope>
    </dependency>
0

Also, consider removing PowerMock, bytebuddy and objenesis dependencies at all. Keep only the following Mockito dependency.

    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-core</artifactId>
        <version>2.7.12</version>
        <scope>test</scope>
    </dependency>

This should fix the error for the most part of the scenarios.

Not the answer you're looking for? Browse other questions tagged or ask your own question.