powermock: Static mocking broken for Mockito >= 2.26.1

When using Powermocks mockStatic and then calling the when(...).thenReturn(...) method combination of Powermock, the thenReturn method will fail due to a change in Mockito 2.26.1. See https://github.com/mockito/mockito/pull/1680/commits/4c3d79c7af8d42e5bb76cd582e932ff28fa84e06#diff-e4b9e3c44b9c51ed8b23dd318259b1aaR51 As you can see the isVoid check has changed to gather some information from the mock contained in the invocation object. Unfortunately this mock is not the mocked object but instead the original class which was used to call the static method. Due to that Mockito complains about this via org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class

About this issue

  • Original URL
  • State: open
  • Created 5 years ago
  • Reactions: 34
  • Comments: 44 (2 by maintainers)

Commits related to this issue

Most upvoted comments

I was having the same issue and the solution for me was to remove the mock maker configuration I had for Mockito: src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker And only use the one for PowerMock: src/test/resources/org/powermock/extensions/configuration.properties Having both at the same time caused the above issue for me no matter what I did or what version I used.

Ok, at last… the solution was: 1- Eliminate line "testImplementation 'org.mockito:mockito-inline:2.13.0' " in graddle 2- Put a file named “configuration.properties” in your project under path “src\test\resources\org\powermock\extensions” with just one line inside “mockito.mock-maker-class=mock-maker-inline

May be there shoud be any kind of libraries conflict when adding it through graddle file

Here is the full documentation: https://github.com/powermock/powermock/wiki/mockito#mockito-mock-maker-inline

Do some have any idea, how to solve this issue?

I’ve solved it for my project like this: stopped using PowerMock at all.

+1 for this issue.

Our test suite runs okay with Mockito 2.23.4 / PowerMock 2.0.0 but fails when upgrading Mockito to 3.0.0:

org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class

The problem still happens on the latest version of powermock and mockito 3.9.

I had the same issue 😦

@jlondonno Use PowerMockito.when(MetricUtil.getNodeName()).thenAnswer((Answer<String>) invocation -> “node1”) instead of thenReturn method.

Hear is the stackoverflow answer… https://stackoverflow.com/questions/62742777/org-mockito-exceptions-misusing-notamockexception-argument-should-be-a-mock-bu

Following this example works for me with 2.0.4

https://github.com/powermock/powermock-examples-maven/blob/master/mockito2/src/test/resources/org/powermock/extensions/configuration.properties

        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-module-junit4</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.powermock</groupId>
            <artifactId>powermock-api-mockito2</artifactId>
            <version>2.0.4</version>
            <scope>test</scope>
        </dependency>

Same, added the configuration.properties per documentation and still getting NotAMockException. Using Mockito 2.23.0 and PowerMock 2.0.4

Our tests run fine with mockito 2.25.0 and powermock 2.0.2.

Ran into this same problem when updating from powermock 2.0.2 to 2.0.4 while leaving mockito at 2.25.0.

Hi @thekingnothing , but that PR does not actually fix the issue with when using the powermock rule. Can we re-open this ticket?

src/test/resources/org/powermock/extensions/configuration.properties: mockito.mock-maker-class=mock-maker-inline

It works for me.

I am using JDK8 + Mockito 3.1.0 + PowerMock 2.0.5 and it’s not working. Tried the mock-maker-inline fix suggested above but no luck. Had to revert to Mockito 2.26.0 to make it work.

I’m having this issue if I add line " testImplementation ‘org.mockito:mockito-inline:2.13.0’ " to gradle.

If I eliminate it it works nice, but I need it to tests final classes and need to add this.

danprado’s solution also worked in our case. After removing src/test/resources/mockito-extensions/org.mockito.plugins.MockMaker and upgrading to Mockito 3.4.4, PowerMock 2.0.7 (Java 11) everything worked fine. Also, we don’t have a src/test/resources/org/powermock/extensions/configuration.properties file, so It might be not necessary at all

@Alinakay30 I think that the best solution is just dropping powermock all together and use alternative libraries.

2 of my tests were not passing with powermock 2.0.2. Many people here said that their tests passed using 2.0.4, i tried it and all my tests passed as well. I am using Mockito 3.6.28 with powermock 2.0.4 and Java 8… Hope it helps someone!

Same Issue when using verifyStatic(). This happens for me also for version 2.11.0