error-prone-support icon indicating copy to clipboard operation
error-prone-support copied to clipboard

Introduce BugPattern for removing duplicate `Mockito.verifyNoInteractions()` calls

Open Kamil-Gabaydullin opened this issue 1 year ago • 3 comments

Problem

When verifying that mocks didn't have interactions in tests sometimes many calls to Mockito.verifyNoInteractions() are used verifyNoInteractions(mock1); verifyNoInteractions(mock2); which unnecessarily pollutes the code, because this method actually accepts varargs. This can be simplified to just: verifyNoInteractions(mock1, mock2);

Description of the proposed new feature

  • [x] Support a stylistic preference.
  • [x] Avoid a common gotcha, or potential problem.
  • [ ] Improve performance.

We should rewrite multiple calls to verifyNoInteractions to one call.

I would like to rewrite the following code:

verifyNoInteractions(mock1);
verifyNoInteractions(mock2);
...
verifyNoInteractions(mockN);

to:

verifyNoInteractions(mock1, mock2, ..., mockN);

Considerations

Tricky parts in my opinion are:

  1. That there might be a variable number of mocks verified in this way.
  2. That multiple individual calls might not be consequential.
  3. That the scope we need to check this must be just inside one test method.

Participation

  • [x] I am willing to submit a pull request to implement this improvement.(I'm willing to try to submit a pr, this will be my first encounter with error prone 🙂)

Kamil-Gabaydullin avatar Jan 24 '23 08:01 Kamil-Gabaydullin