Skip to content

Static mocking broken for Mockito >= 2.26.1 #992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
GFriedrich opened this issue Jun 26, 2019 · 47 comments · Fixed by #997
Open

Static mocking broken for Mockito >= 2.26.1 #992

GFriedrich opened this issue Jun 26, 2019 · 47 comments · Fixed by #997

Comments

@GFriedrich
Copy link

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 mockito/mockito@4c3d79c#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

@duyp
Copy link

duyp commented Jul 4, 2019

I had the same issue :(

@SimonHaenle2
Copy link

SimonHaenle2 commented Jul 4, 2019

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

@ppamorim
Copy link

Same issue.

@rratmansky
Copy link

I'm also seeing this issue with verifyStatic() calls. I've done some digging, and it looks like the classloader that is used to create the mock is not the same one used when we try to verifystatic. This can be seen because the hash code of the class object changed. If you step through the mock creation and the verification (when mockito puts/gets the mock from the mocks map in InlineByteBuddyMockMaker.Java) you can see that the hash codes are different.

@TehBakker
Copy link

Same issue ... We just migrated our project to springboot2 and thus got all the version upgrade and got these issue.
The "WHEN" issue with static method seems indeed to be related to 2.26.1 as @GFriedrich noted, since forcing version to 2.26.0 I can now by pass the "WHEN".
Only to crash on the "VERIFY" of the same method, with the same issue
_

org.mockito.exceptions.misusing.NotAMockException:
Argument passed to verify() is of type Class and is not a mock!

_

@rratmansky
Copy link

I was wrong in my previous statement about the issue being caused by different classloaders. I just spent the last 4 hours debugging, and the real issue is that mockito is storing an instance of the static class in the mocks map, and verify is passing in the static class object to verify. This results in mockito saying it doesn't have a mock to verify against. I'm looking to see what can be done, although it might require changes in mockito and not powermock.

@rratmansky
Copy link

I was able to get verifyStatic working with mockito 2.26.0 by forking mockito. It likely will only work if you are using the inline bytebuddy mock maker, which is what I'm using. I figured I'd share here in case someone has the time to make it work from the powermock side instead of the mockito side. The change I made was to add the following code to the createMock() of org/mockito/internal/creation/bytebuddy/InlineByteBuddyMockMaker.java (around line 183):

mocks.put(instance.getClass(), mockMethodInterceptor);

@rratmansky
Copy link

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

@sam-ma
Copy link
Contributor

sam-ma commented Sep 2, 2019

@rratmansky Sorry for the late response.
It works for me when testing with powermock rule.I was testing tests/mockito/junit4/src/test/java/samples/powermockito/junit4/system/SystemClassUserTest.java by doing the follows

  1. copy it into "tests/mockito/junit4-rule-xstream" project (which provides necessary test dependencies)
  2. remove @RunWith(PowerMockRunner.class)
  3. add "@rule public final PowerMockRule rule = new PowerMockRule();"

Would you mind elaborating?

@nEdAy
Copy link

nEdAy commented Sep 17, 2019

I had the same issue :(

@nEdAy
Copy link

nEdAy commented Sep 17, 2019

  1. mockStatic —— when(...).thenReturn(...)
  2. com.nhaarman.mockitokotlin2 —— Mockito.any(T::class.java) ?: createInstance()

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

@MihaiBojin
Copy link

+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

darxriggs added a commit to darxriggs/influxdb-plugin that referenced this issue Oct 7, 2019
Now the retrieval of a `Descriptor` instance is similar in `InfluxDbPublisher` and `InfluxDbStep`.

The change requires mocking of the static method `Jenkins#getInstance`.
Issue powermock/powermock#992 was hit along the way.
Therefore the Mockito dependency has to be downgraded slightly.
odl-github pushed a commit to opendaylight/odlparent that referenced this issue Oct 22, 2019
No official release notes (yet?), the following issues are fixed:
powermock/powermock#685
powermock/powermock#992
powermock/powermock#939

Change-Id: Ie8266a961c75cb0dcb606e4639ef0b91a8992b5e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
odl-github pushed a commit to opendaylight/odlparent that referenced this issue Oct 23, 2019
No official release notes (yet?), the following issues are fixed:
powermock/powermock#685
powermock/powermock#992
powermock/powermock#939

Change-Id: Ie8266a961c75cb0dcb606e4639ef0b91a8992b5e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
(cherry picked from commit 3479902)
@sjr
Copy link

sjr commented Oct 29, 2019

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.

@JCallejaarSantander
Copy link

JCallejaarSantander commented Nov 8, 2019

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.

@JCallejaarSantander
Copy link

JCallejaarSantander commented Nov 8, 2019

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

@TehBakker
Copy link

Sorry I see this has been fixed then re-opened ?
If i up to 2.0.2 or 2.0.4 I still have org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class on my when call.
=> Had to update verifyStatic signature to specifiy the class, which gave me hope.

@helenchen-okta
Copy link

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

@ArtemHoi
Copy link

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

@wlsc
Copy link

wlsc commented Jan 29, 2020

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

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

@baotpham
Copy link

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>

@ArtemHoi
Copy link

It's don't work with java 13

@esfomeado
Copy link

esfomeado commented May 22, 2021

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

@vladkabat
Copy link

vladkabat commented Jul 17, 2021

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

It works for me.

@Alinakay30
Copy link

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!

@esfomeado
Copy link

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

@iamarjun
Copy link

@jlondonno Use PowerMockito.when(MetricUtil.getNodeName()).thenAnswer((Answer) 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

getting the same error on these lines

PowerMockito.spy(LogUtils::class.java)
PowerMockito.doNothing().`when`(LogUtils::class.java, "logException", any())

@mzhua
Copy link

mzhua commented Dec 19, 2021

now i use thenAnswer instead

@NashMiao
Copy link

with powermock 2.0.9 and mockito 3.3.3, this bug still exsit

alluxio-bot pushed a commit to Alluxio/alluxio that referenced this issue Mar 10, 2022

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
### What changes are proposed in this pull request?

Replace PowerMock with Mockito in LogLevelTest.

### Why are the changes needed?

Part of #15003.

This PR intentionally limits the scope of change to a single low-impact
test, so that we can first add needed dependencies and see if it works,
then proceed to replace more PowerMock in other tests.

### Does this PR introduce any user facing changes?

No.

### Why not set mockito-inline in all child-projects but only apply this
dependency to the `shell` project?

`powermock` is still used in other child projects. If the
`mockito-inline` dependency is enabled for all child projects in root
pom, because of powermock/powermock#992, there
will be conflicts between powermock and mockito-inline.

The plan is to progressively replace powermock module-by-module, then
remove powermock from root pom and enable mockito-inline there.

pr-link: #15091
change-id: cid-e2b32959d544ec2697d498a37e0c53186b18267e
@janst44
Copy link

janst44 commented Jul 14, 2022

@jlondonno Use PowerMockito.when(MetricUtil.getNodeName()).thenAnswer((Answer) 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

getting the same error on these lines

PowerMockito.spy(LogUtils::class.java)
PowerMockito.doNothing().`when`(LogUtils::class.java, "logException", any())

@iamarjun I updated the SO answer you linked to include my work around.

@WarrenCooper-sag
Copy link

WarrenCooper-sag commented Jul 14, 2022 via email

zrhoffman added a commit to zrhoffman/trafficcontrol that referenced this issue Sep 15, 2022

Verified

This commit was signed with the committer’s verified signature.
zrhoffman Zach Hoffman
zrhoffman added a commit to zrhoffman/trafficcontrol that referenced this issue Oct 2, 2022

Verified

This commit was signed with the committer’s verified signature.
zrhoffman Zach Hoffman
fathonyfath added a commit to fathonyfath/react-native that referenced this issue Aug 10, 2023
@SomberOfShadow
Copy link

Same issue with powermock 2.0.9 and mockito 3.12.4

@SomberOfShadow
Copy link

Same issue (powermock 2.0.9 and mockito 3.12.4) when use power.verifyStatic() ,but it works well with Mockito

@yzpnet
Copy link

yzpnet commented Dec 26, 2023

Same issue with powermock 2.0.9 and mockito 3.11.2

@Higan
Copy link

Higan commented Jan 11, 2024

Same issue here with powermock 2.0.9 and mockito 2.28.2

@liuhjhj
Copy link

liuhjhj commented Mar 20, 2024

Same issue here with powermock 2.0.9 and mockito 3.8.0

@1tmb
Copy link

1tmb commented May 4, 2024

I arrived here via https://stackoverflow.com/questions/58752923/notamockexception-when-trying-to-verify-a-static-method

Using v 2.0.9 I was able to work around this by:

  1. Creating a new file at the path src/test/resources/org/powermock/extensions/configuration.properties and including the key value pair mockito.mock-maker-class=mock-maker-inline
  2. Deleting a previous configuration file which I had at the path src/test/resources/mockito-extensions/ org.mockito.plugins.MockMaker which previously included the phase mock-maker-line

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.