-
Notifications
You must be signed in to change notification settings - Fork 72
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
Consider logic change of mockito argument matchers #319
Comments
Good that you've pointed out these specifics; I've extracted the below table from the 5.1.1 documentation for clarity:
With this further note on any(Class type):
|
Are you proposing then that for the migration from 1.x to 3.x+ we swap As a developer I'd be mildly surprised about such a change, or at least until I read up on the specifics like we see above. It's not been pointed out as a bug before; but perhaps like you said I imagine this would be a yaml only change in Mockito1to3Migration, which could make this relatively straightforward. |
We did a huge Mockito upgrade in our company with million lines of code. The main root cause for the afterwards failing tests was this semantic change. We weren't aware of it upfront and so the affected colleagues did search and replace in their IDEs which costs a lot of time. So having this fix would be beneficial for all upcoming Mockito upgrades. |
Yes sorry to hear this particular aspect of that migration has not been as smooth as you would have hoped; I think just inserting - org.openrewrite.java.ChangeMethodName:
methodPattern: org.mockito.ArgumentMatchers any(java.lang.Class)
newMethodName: nullable into You're also able to run this change separately with a temporary |
I'm not sure how to rewrite |
No need to be sorry! We grateful for all your help, both in thorough contributions such as the PowerMock recipe, as well as detailed reports on imperfections such as this one. I've added the |
Picked up as a quick attempt in #320; welcome to look that over, but no requirement whatsoever. :) |
…320) * Replace Mockito 1.x any(Class) and anyString() with nullable(Class) Fixes #319 * fix error of missing types * improve performance * Update src/main/java/org/openrewrite/java/testing/mockito/AnyStringToNullable.java Co-authored-by: Tim te Beek <[email protected]> * pass CI * Verify AnyToNullable * Require the old mockito-all to be present before changes * Restore JunitMockitoUpgradeIntegrationTest * Add UsesMockitoAll to workaround anySource AND usage As kindly suggested by @kunli2 --------- Co-authored-by: Kun Li <[email protected]> Co-authored-by: Kun Li <[email protected]>
From Mockito 1.x to 3.x the logic of the argument matchers has changed. Mockito.any() doesn't match a null value that is passed to the mocked method as parameter. (see https://site.mockito.org/javadoc/current/org/mockito/ArgumentMatchers.html and https://javadoc.io/static/org.mockito/mockito-core/5.1.1/org/mockito/ArgumentMatchers.html#nullable(java.lang.Class))
Therefore, it is necessary to replace all instances of Mockito.anyString() and any() with nullable(String) or nullable(). Nullable behaves exactly like anyString() or any() in the previous Mockito version 1.x.
The text was updated successfully, but these errors were encountered: