Open GFriedrich opened 5 years ago
I had the same issue :(
Same Issue when using verifyStatic(). This happens for me also for version 2.11.0
Same issue.
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.
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!
_
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.
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);
Hi @thekingnothing , but that PR does not actually fix the issue with when using the powermock rule. Can we re-open this ticket?
@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
@RunWith(PowerMockRunner.class)
Would you mind elaborating?
I had the same issue :(
org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class
+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
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.
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.
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
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.
Same, added the configuration.properties
per documentation and still getting NotAMockException
. Using Mockito 2.23.0 and PowerMock 2.0.4
Do some have any idea, how to solve this issue?
Do some have any idea, how to solve this issue?
I've solved it for my project like this: stopped using PowerMock at all.
Following this example works for me with 2.0.4
<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>
It's don't work with java 13
I use JDK13 + Mockito 3.2.4 + PowerMock 2.0.5 on my project, all unit tests pass. (https://github.com/vividus-framework/vividus)
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 solve this problem by remove @PowerMockIgnore ( "android.","androidx.")
I'm seeing the same issue and also had to revert to an older version to get around it.
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.
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
Hey @dsd3mon. Thanks for the input. At least with the versions I'm using (Mockito 2.21.0 and PowerMock 2.0.0) the PowerMock mock maker configuration is still needed to spy final classes. If you don't spy final classes, then you shouldn't need that. Just throwing it in here in case someone wonders why that is needed. Happy coding!
@danprado @dsd3mon first of all, thanks in advance for the attention and comments you can provide. I have been struggling with the same problem and even I have tested my code with this setup: Mockito 3.4.4, PowerMock 2.0.7 (Java 11) and my code continue throwing the same exception:
org.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class
Do you think there are another things I can check out?
@jlondonno Use PowerMockito.when(MetricUtil.getNodeName()).thenAnswer((Answer
Hear is the stackoverflow answer... https://stackoverflow.com/questions/62742777/org-mockito-exceptions-misusing-notamockexception-argument-should-be-a-mock-bu
@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
Heyy @xiaohuifirst , sorry for the delay. I want to thank for this tip, it was really useful. Thank you very much.
Neither I removed src/test/resources/mockito-extensions/org.mockito.plugins.MockMake nor I had src/test/resources/org/powermock/extensions/configuration.properties. I didn't either change thenReturn() to thenAnswer(). The only fix solved the issue was to set Mockito to 3.4.4. Thanks @dsd3mon. I had powermock 2.0.5 beta and Java 11.
The problem still happens on the latest version of powermock and mockito 3.9.
src/test/resources/org/powermock/extensions/configuration.properties: mockito.mock-maker-class=mock-maker-inline
It works for me.
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!
@Alinakay30 I think that the best solution is just dropping powermock all together and use alternative libraries.
@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())
now i use thenAnswer instead
with powermock 2.0.9 and mockito 3.3.3, this bug still exsit
@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.
Please Unsubscribe -ty
Best regards Warren Cooper Principal Architect T +1-571-262-7738 @.**@.> Software AG USA Inc, 11700 Plaza America Drive, Suite 700, Reston, VA 20190, United States of America
@.http://www.softwareag.com/ [Icon Description automatically generated]https://www.linkedin.com/company/software-ag @.https://twitter.com/SoftwareAG @.https://www.youtube.com/c/SOFTWAREAGglobal @.https://www.instagram.com/softwareag_life/ @.***https://www.facebook.com/SoftwareAG
@.***
From: Joshua Campbell @.> Sent: Thursday, July 14, 2022 12:09 PM To: powermock/powermock @.> Cc: Subscribed @.***> Subject: Re: [powermock/powermock] Static mocking broken for Mockito >= 2.26.1 (#992)
@jlondonnohttps://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fjlondonno&data=05%7C01%7Cwarren.cooper%40softwareag.com%7Cb071d57cc2214ac5d44108da65b33421%7Cd9662eb9ad984e74a8a204ed5d544db6%7C1%7C0%7C637934117649746403%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=W6a7fii%2Fmp0vvi24yznSnUnXwa9VHP8naoR4cCjOI7s%3D&reserved=0 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-buhttps://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstackoverflow.com%2Fquestions%2F62742777%2Forg-mockito-exceptions-misusing-notamockexception-argument-should-be-a-mock-bu&data=05%7C01%7Cwarren.cooper%40softwareag.com%7Cb071d57cc2214ac5d44108da65b33421%7Cd9662eb9ad984e74a8a204ed5d544db6%7C1%7C0%7C637934117649746403%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=eGLqicJ8PmtaI%2Bwg917v4yIc9rqfiSguPbiRI7s87oA%3D&reserved=0
getting the same error on these lines
PowerMockito.spy(LogUtils::class.java)
PowerMockito.doNothing().when
(LogUtils::class.java, "logException", any())
I updated the SO answer you linked to include my work around.
- Reply to this email directly, view it on GitHubhttps://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fpowermock%2Fpowermock%2Fissues%2F992%23issuecomment-1184628619&data=05%7C01%7Cwarren.cooper%40softwareag.com%7Cb071d57cc2214ac5d44108da65b33421%7Cd9662eb9ad984e74a8a204ed5d544db6%7C1%7C0%7C637934117649746403%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=5YXn%2BA87nzdN%2FAtNB7XQisULqz8ePOHkGrwUiW%2FKyGk%3D&reserved=0, or unsubscribehttps://eur04.safelinks.protection.outlook.com/?url=https%3A%2F%2Fgithub.com%2Fnotifications%2Funsubscribe-auth%2FAJFOGSYBECSOPD6E4IIG7B3VUA3SZANCNFSM4H3R42VA&data=05%7C01%7Cwarren.cooper%40softwareag.com%7Cb071d57cc2214ac5d44108da65b33421%7Cd9662eb9ad984e74a8a204ed5d544db6%7C1%7C0%7C637934117649746403%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=gvuBeBIElD5V%2BW8ws0EcPNcbiK0WmiKTP4MdGzshRXg%3D&reserved=0. You are receiving this because you are subscribed to this thread.Message ID: @.***>
Same issue with powermock 2.0.9 and mockito 3.12.4
Same issue (powermock 2.0.9 and mockito 3.12.4) when use power.verifyStatic() ,but it works well with Mockito
Same issue with powermock 2.0.9 and mockito 3.11.2
Same issue here with powermock 2.0.9 and mockito 2.28.2
Same issue here with powermock 2.0.9 and mockito 3.8.0
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:
src/test/resources/org/powermock/extensions/configuration.properties
and including the key value pair mockito.mock-maker-class=mock-maker-inline
src/test/resources/mockito-extensions/ org.mockito.plugins.MockMaker
which previously included the phase mock-maker-line
When using Powermocks
mockStatic
and then calling thewhen(...).thenReturn(...)
method combination of Powermock, thethenReturn
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 theisVoid
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 viaorg.mockito.exceptions.misusing.NotAMockException: Argument should be a mock, but is: class java.lang.Class