powermock / powermock

PowerMock is a Java framework that allows you to unit test code normally regarded as untestable.
Apache License 2.0
4.16k stars 586 forks source link

Static mocking broken for Mockito >= 2.26.1 #992

Open GFriedrich opened 5 years ago

GFriedrich commented 5 years ago

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

duyp commented 5 years ago

I had the same issue :(

SimonHaenle2 commented 5 years ago

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

ppamorim commented 5 years ago

Same issue.

rratmansky commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

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 commented 5 years ago

@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 commented 4 years ago

I had the same issue :(

nEdAy commented 4 years ago
  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 commented 4 years ago

+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
sjr commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

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

ArtemHoi commented 4 years ago

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

wlsc commented 4 years ago

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 commented 4 years ago

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 commented 4 years ago

It's don't work with java 13

valfirst commented 4 years ago

I use JDK13 + Mockito 3.2.4 + PowerMock 2.0.5 on my project, all unit tests pass. (https://github.com/vividus-framework/vividus)

katsik commented 4 years ago

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.

doubleYellow commented 4 years ago

i solve this problem by remove @PowerMockIgnore ( "android.","androidx.")

SteveDeChristopher commented 4 years ago

I'm seeing the same issue and also had to revert to an older version to get around it.

danprado commented 4 years ago

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.

dsd3mon commented 4 years ago

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

danprado commented 4 years ago

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!

jlondonno commented 4 years ago

@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?

loginhel commented 4 years ago

@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

jlondonno commented 4 years ago

@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.

lakshminarasimhanb commented 3 years ago

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.

esfomeado commented 3 years ago

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

vladkabat commented 3 years ago

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

It works for me.

Alinakay30 commented 3 years ago

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 commented 3 years ago

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

iamarjun commented 2 years ago

@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 commented 2 years ago

now i use thenAnswer instead

NashMiao commented 2 years ago

with powermock 2.0.9 and mockito 3.3.3, this bug still exsit

janst44 commented 2 years ago

@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 commented 2 years ago

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

@.***

@.***https://www.softwareag.com/en_corporate/company/connected-enterprise.html?at=navpromo_about?utm_source=sales&utm_medium=email&utm_region=hq&utm_campaign=swag-brand_umbrella&utm_subcampaign=empower_email_sig&utm_content=webpage_tce

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: @.***>

SomberOfShadow commented 9 months ago

Same issue with powermock 2.0.9 and mockito 3.12.4

SomberOfShadow commented 9 months ago

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

yzpnet commented 8 months ago

Same issue with powermock 2.0.9 and mockito 3.11.2

Higan commented 8 months ago

Same issue here with powermock 2.0.9 and mockito 2.28.2

liuhjhj commented 5 months ago

Same issue here with powermock 2.0.9 and mockito 3.8.0

1tmb commented 4 months ago

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