89 lines
3.2 KiB
Plaintext
89 lines
3.2 KiB
Plaintext
== Why is this an issue?
|
|
|
|
Mockito provides _argument matchers_ and _argument captors_ for flexibly stubbing or verifying method calls.
|
|
|
|
`Mockito.verify()`, `Mockito.when()`, `Stubber.when()` and `BDDMockito.given()` each have overloads with and without argument matchers.
|
|
|
|
However, if argument matchers or captors are used only on some of the parameters, all the parameters need to have matchers as well, otherwise an `InvalidUseOfMatchersException` will be thrown.
|
|
|
|
This rule consequently raises an issue every time matchers are not used on all the parameters of a stubbed/verified method.
|
|
|
|
== How to fix it
|
|
|
|
=== Code examples
|
|
|
|
==== Noncompliant code example
|
|
[source,java,diff-id=1,diff-type=noncompliant]
|
|
----
|
|
@Test
|
|
public void myTest() {
|
|
// Setting up mock responses
|
|
given(foo.bar(anyInt(), i1, i2)).willReturn(null); // Noncompliant, no matchers for "i1" and "i2"
|
|
when(foo.baz(eq(val1), val2)).thenReturn("hi"); // Noncompliant, no matcher for "val2"
|
|
|
|
// Simulating exceptions
|
|
doThrow(new RuntimeException()).when(foo).quux(intThat(x -> x >= 42), -1); // Noncompliant, no matcher for "-1"
|
|
|
|
// Verifying method invocations
|
|
verify(foo).bar(i1, anyInt(), i2); // Noncompliant, no matchers for "i1" and "i2"
|
|
|
|
// Capturing arguments for verification
|
|
ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);
|
|
verify(foo).bar(captor.capture(), i1, any()); // Noncompliant, no matchers for "i1"
|
|
}
|
|
----
|
|
|
|
==== Compliant solution
|
|
[source,java,diff-id=1,diff-type=compliant]
|
|
----
|
|
@Test
|
|
public void myTest() {
|
|
// Setting up mock responses
|
|
given(foo.bar(anyInt(), eq(i1), eq(i2))).willReturn(null); // Compliant, all arguments have matchers
|
|
when(foo.baz(val1, val2)).thenReturn("hi"); // Compliant, no argument has matchers
|
|
|
|
// Simulating exceptions
|
|
doThrow(new RuntimeException()).when(foo).quux(intThat(x -> x >= 42), eq(-1)); // Compliant, all arguments have matchers
|
|
|
|
// Verifying method invocations
|
|
verify(foo).bar(eq(i1), anyInt(), eq(i2)); // Compliant, all arguments have matchers
|
|
|
|
// Capturing arguments for verification
|
|
ArgumentCaptor<Integer> captor = ArgumentCaptor.forClass(Integer.class);
|
|
verify(foo).bar(captor.capture(), any(), any()); // Compliant, all arguments have matchers
|
|
}
|
|
----
|
|
|
|
== Resources
|
|
|
|
* https://javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/Mockito.html#argument_matchers[Mockito documentation] - argument matchers
|
|
* https://sonarsource.github.io/rspec/#/rspec/S6068/java[S6068 - Call to Mockito method "verify", "when" or "given" should be simplified]
|
|
|
|
|
|
ifdef::env-github,rspecator-view[]
|
|
|
|
'''
|
|
== Implementation Specification
|
|
(visible only on this page)
|
|
|
|
=== Message
|
|
|
|
Add an "eq()" argument matcher on this/these parameters
|
|
|
|
=== Highlighting
|
|
|
|
primary: the first parameter without argument matchers
|
|
|
|
secondary: all the other parameters without argument matchers
|
|
|
|
'''
|
|
== Comments And Links
|
|
(visible only on this page)
|
|
|
|
=== relates to: S6068
|
|
|
|
=== on 3 Dec 2020, 10:23:27 Quentin Jaquier wrote:
|
|
Note that this is a low priority rule as tests would fail if this bug is present. It can however be useful for SonarLint users as they will be able to see their mistakes more rapidly. It won't add much value for SonarQube/SonarCloud users.
|
|
|
|
endif::env-github,rspecator-view[]
|