-
Notifications
You must be signed in to change notification settings - Fork 39
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
Introduce BugPattern for removing duplicate Mockito.verifyNoInteractions()
calls
#476
base: master
Are you sure you want to change the base?
Introduce BugPattern for removing duplicate Mockito.verifyNoInteractions()
calls
#476
Conversation
return Description.NO_MATCH; | ||
} | ||
var verifyNoInteractionsInvocations = getVerifyNoInteractionsInvocations(tree); | ||
if (verifyNoInteractionsInvocations.size() < 2) { |
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.
If there is only one call to verifyNoInteractions()
we don't want to do anything too
fixBuilder.replace( | ||
ASTHelpers.getStartPosition(invocationTree), | ||
state.getEndPosition(invocationTree) + 1, | ||
""); |
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.
This replacement leaves an empty line behind, I couldn't figure out how to collapse lines 😞 or perhaps it should be taken care of by formatting and it's not a big deal?
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
""); | ||
} | ||
}); | ||
fixBuilder.replace(lastInvocation, "verifyNoInteractions(" + combinedArgument + ")"); |
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.
I've decided that adding it as a last line in a test might not be preferable and thus replacing the last invocation is better, since it preserved intended order and place of this verification.
...ntrib/src/main/java/tech/picnic/errorprone/bugpatterns/MockitoVerifyNoInteractionsUsage.java
Outdated
Show resolved
Hide resolved
" @Test", | ||
" void m() {", | ||
" Runnable runnable1 = mock(Runnable.class);", | ||
"", |
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.
To the point that there are empty lines left behind 😞
" Runnable runnable3 = mock(Runnable.class);", | ||
" verifyNoInteractions(runnable2, runnable3);", | ||
" Runnable runnable4 = mock(Runnable.class);", | ||
" Runnable runnable5 = mock(Runnable.class);", |
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.
Perhaps there is a less verbose way to test this 🤔
...ntrib/src/main/java/tech/picnic/errorprone/bugpatterns/MockitoVerifyNoInteractionsUsage.java
Show resolved
Hide resolved
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.
Hey @Kamil-Gabaydullin , thanks for opening the PR! Looks cool 🚀 !
I left a few suggestions on the PR 😄. I have to do some expectation management. The are a lot of PRs in review so I'm not sure when one of us gets around to doing a in depth review...
We'll keep you up to date for sure.
...b/src/test/java/tech/picnic/errorprone/bugpatterns/MockitoVerifyNoInteractionsUsageTest.java
Outdated
Show resolved
Hide resolved
...ntrib/src/main/java/tech/picnic/errorprone/bugpatterns/MockitoVerifyNoInteractionsUsage.java
Outdated
Show resolved
Hide resolved
...ntrib/src/main/java/tech/picnic/errorprone/bugpatterns/MockitoVerifyNoInteractionsUsage.java
Outdated
Show resolved
Hide resolved
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
2 similar comments
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
a17bfff
to
b5f99d5
Compare
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
No idea if something can be done with this warning that |
b5f99d5
to
301ec17
Compare
Mutation testing report by Pitest. Review any surviving mutants by inspecting the line comments under Files changed. |
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 contributing to Error Prone Support, @Kamil-Gabaydullin!
I haven't deeply looked into the PR yet, but I'm seeing parallels with #443: in both cases we want to merge adjacent method calls. Other examples might include e.g. addAll
builder methods. So I'm thinking that perhaps a more generic check is hiding in this code 👀
" Mockito.verifyNoInteractions(mock2, mock3);", | ||
" Object mock4 = mock(Object.class);", | ||
" String str = mock1.toString();", | ||
" Object mock5 = mock(Object.class);", | ||
" Mockito.verifyNoInteractions(mock4);", |
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.
In general, collapsing two non-adjacent verifyNoInteractions
calls changes the semantics of the code, as the deferred verification could cause a test failure.
verifyNoInteractionsInvocations.stream() | ||
.map(MethodInvocationTree::getArguments) | ||
.flatMap(List::stream) | ||
.map(Object::toString) |
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.
👀 looks like we're converting a Tree
to String
here. For that we should use SourceCode.treeToString
.
Hi @Kamil-Gabaydullin, for a while there was no activity on this PR. Partly because @Stephan202 and I were swamped and reviewing PRs that contain a new BugPattern can take quite some time. Still, having to wait for a long time for reviews is not nice. This situation is far from ideal and we want to do better. Hence, we set up an (internal) review pool to have others help out with reviewing PRs. Concretely this means we want to continue where we left off and start reviewing this again. Therefore, we'd like to discuss the state and direction of the PR. Stephan flagged in this comment that the current state could lead to semantic changes and therefore we need to change some things. There are some things we need to consider to determine how we want to continue with this PR. There are a few options:
So the question is, are you still up for applying changes to get it into a state where we can start reviewing again? If yes, which direction do you want to go in? |
Yep, I've been thinking for a while about this one after Stephan's comments. For the implementation, I think we should run this This doesn't seem like a "good first issue" anymore though 😁 I'm okay to try implementing this (again), but I have my doubts 😅 |
We have a few BugPatterns where we have a curated list of calls that we want to match, see StaticImport or IdentityConversion. So that will not be a problem.
Yeah I can imagine, this will not be an easy one. We could leave this one for now and maybe @Stephan202 or I will pick it up. We can definitely find you another BugPattern if you are interested, one that fits the category |
Sounds good, I'll have a look around and pick something when I have the time. |
This pr adds a
BugPattern
to recognize multipleverifyNoInteractions()
calls in test methods.I'll leave comments throughout the pr to address questions and decisions.