-
Notifications
You must be signed in to change notification settings - Fork 586
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
Comments
I had the same issue :( |
Same Issue when using verifyStatic(). |
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.
_ |
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):
|
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.
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
|
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.
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>
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)
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: May be there shoud be any kind of libraries conflict when adding it through graddle file Here is the full documentation: |
Sorry I see this has been fixed then re-opened ? |
Same, added the |
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
|
It's don't work with java 13 |
The problem still happens on the latest version of powermock and mockito 3.9. |
src/test/resources/org/powermock/extensions/configuration.properties: 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. |
getting the same error on these lines
|
now i use thenAnswer instead |
with powermock 2.0.9 and mockito 3.3.3, this bug still exsit |
### 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
@iamarjun I updated the SO answer you linked to include my work around. |
Reference from this github issue: powermock/powermock#992
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
|
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 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
The text was updated successfully, but these errors were encountered: