GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fixes #2548 : Makes InOrder able to verify static methods by mirkoalicastro · Pull Request #2549 · mockito/mockito

Conversation

@mirkoalicastro

@mirkoalicastro mirkoalicastro commented Jan 24, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This PR makes it possible to verify static methods calls in order. Fixes: #2548

Example of usage:

try (MockedStatic<Foo> mocked = mockStatic(Foo.class)) {
  InOrder inOrder = inOrder(Foo.class);
  mocked.when(Foo::firstMethod).thenReturn("first");
  assertEquals("first", Foo.firstMethod());

  inOrder.verify(mocked, Foo::firstMethod);
}

You can also pass a specific VerificationMode as third argument to the InOrder#verify method.

Checklist

  • Read the contributing guide
  • PR should be motivated, i.e. what does it fix, why, and if relevant how
  • If possible / relevant include an example in the description, that could help all readers
    including project members to get a better picture of the change
  • Avoid other runtime dependencies
  • Meaningful commit history ; intention is important please rebase your commit history so that each
    commit is meaningful and help the people that will explore a change in 2 years
  • The pull request follows coding style
  • Mention Fixes #<issue number> in the description if relevant
  • At least one commit should mention Fixes #<issue number> if relevant

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
@mirkoalicastro
Fixes mockito#2548 : Makes InOrder able to verify static methods
f15b566
@mirkoalicastro mirkoalicastro mentioned this pull request Jan 24, 2022
[PR open] Add feature to verify static methods calls in order #2548
Closed
@mirkoalicastro
mirkoalicastro marked this pull request as ready for review February 1, 2022 21:30
TimvdLippe
TimvdLippe suggested changes Feb 15, 2022
View reviewed changes

@TimvdLippe TimvdLippe left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! Overall, the implementation looks solid. I only have a couple of nits with regards to the tests.

My only concern is the difference in the class passed into inOrder and the class we are actually interacting with. I guess this currently works, as we are passing in a Class in the List<Object>, but that feels a bit weird to me. Not sure WDYT?

I think to fix that, we would need a similar method to objectIsMockToBeVerified, but in this case verify that the context is correct. However, we are only passing in the lambda, which doesn't necessarily match to the Class we put in.

Therefore, we maybe have to look into passing the mockedStatic in the inOrder method instead and omit it as an argument from the inOrder.verify call?

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
Comment thread subprojects/inline/src/test/java/org/mockitoinline/InOrderVerificationTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread subprojects/inline/src/test/java/org/mockitoinline/InOrderVerificationTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread subprojects/inline/src/test/java/org/mockitoinline/InOrderVerificationTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread subprojects/inline/src/test/java/org/mockitoinline/InOrderVerificationTest.java Resolved

Uh oh!

There was an error while loading. Please reload this page.

@mirkoalicastro
Refactors unit tests
f66ab4f
@mirkoalicastro

mirkoalicastro commented Feb 16, 2022

Copy link
Copy Markdown
Contributor Author

Thanks for the PR! Overall, the implementation looks solid. I only have a couple of nits with regards to the tests.

My only concern is the difference in the class passed into inOrder and the class we are actually interacting with. I guess this currently works, as we are passing in a Class in the List<Object>, but that feels a bit weird to me. Not sure WDYT?

I think to fix that, we would need a similar method to objectIsMockToBeVerified, but in this case verify that the context is correct. However, we are only passing in the lambda, which doesn't necessarily match to the Class we put in.

Therefore, we maybe have to look into passing the mockedStatic in the inOrder method instead and omit it as an argument from the inOrder.verify call?

Thanks for your feedback @TimvdLippe ! I have addressed the comments and pushed the changes related to the tests.

I see your concern, and actually, passing the mockedStatic to the inOrder method was my initial idea. Here is the reason why I moved away from that.
Let's assume we have two static mocks (two different classes), and we want to verify them in order. Then, we'd pass two MockedStatic instances to the inOrder method.
When it comes to the verification, we'd have InOrder#verify(Verification), where Verification is a lambda (no parameters, void return type) because MockedStatic#verify deals with a lambda. Given a lambda, we can't assert if it contains a static method reference, or contains just some statements. That is, we can't retrieve the Class instance. Without this, we can't select and use the appropriate MockedStatic instance to verify the lambda.
Moreover, the above assumes we have the Class instance of the MockedStatic somehow available in InOrderImpl, but currently, that is not the case. Of course, we could add a method to MockedStatic that accepts a Class and returns true if the Class is the same as what is mocking (package private maybe?).
I see your concern, but I think we have the same situation in the MockedStatic#verify methods, where we could pass things not relevant to the context. But I can't see a different way to solve it otherwise, if possible.
WDYT?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@mirkoalicastro
Fixes spotless violations
94153ec
@codecov-commenter

codecov-commenter commented Feb 16, 2022 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Codecov Report

Merging #2549 (fd2b2b6) into main (8cdf0cc) will increase coverage by 0.04%.
The diff coverage is 100.00%.

❗ Current head fd2b2b6 differs from pull request most recent head 94153ec. Consider uploading reports for the commit 94153ec to get more accurate results

Impacted file tree graph

@@             Coverage Diff              @@
##               main    #2549      +/-   ##
============================================
+ Coverage     86.71%   86.75%   +0.04%     
- Complexity     2784     2796      +12     
============================================
  Files           320      321       +1     
  Lines          8341     8368      +27     
  Branches       1021     1024       +3     
============================================
+ Hits           7233     7260      +27     
  Misses          840      840              
  Partials        268      268              
Impacted Files Coverage Δ
src/main/java/org/mockito/InOrder.java 100.00% <100.00%> (ø)
...rc/main/java/org/mockito/internal/InOrderImpl.java 100.00% <100.00%> (ø)
...kito/internal/stubbing/answers/InvocationInfo.java 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8cdf0cc...94153ec. Read the comment docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

TimvdLippe
TimvdLippe approved these changes Feb 16, 2022
View reviewed changes

@TimvdLippe TimvdLippe left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome, thanks for the explanation (makes sense to me) and the nice PR + tests! Merging 🎉

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mirkoalicastro reacted with rocket emoji
All reactions
  • 🚀 1 reaction
@TimvdLippe
TimvdLippe merged commit 02d6356 into mockito:main Feb 16, 2022
@theaspect theaspect mentioned this pull request Mar 15, 2022
Bump mockito-core from 4.3.1 to 4.4.0 theaspect/sandbox#41
Merged
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

1 more reviewer
@TimvdLippe TimvdLippe TimvdLippe approved these changes
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

[PR open] Add feature to verify static methods calls in order

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@mirkoalicastro @codecov-commenter @TimvdLippe
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.