MockitoStubbing

SUGGESTION

Simplification

View source code on GitHub

Summary

Don’t unnecessarily use Mockito’s eq(...)

Suppression

Suppress false positives by adding the suppression annotation @SuppressWarnings("MockitoStubbing") to the enclosing element.

Disable this pattern completely by adding -Xep:MockitoStubbing:OFF as compiler argument. Learn more.

Samples

Replacement

Shows the difference in example code before and after the bug pattern is applied.

 import static org.mockito.ArgumentMatchers.eq;
 import static org.mockito.Mockito.doAnswer;
 import static org.mockito.Mockito.mock;
 
 import java.util.function.BiConsumer;
 import java.util.function.Consumer;
 import org.mockito.ArgumentMatchers;
 
 class A {
   void m() {
     Consumer<String> consumer = mock(Consumer.class);
-    doAnswer(inv -> null).when(consumer).accept(ArgumentMatchers.eq("foo"));
-    doAnswer(inv -> null).when(consumer).accept(eq(toString()));
+    doAnswer(inv -> null).when(consumer).accept("foo");
+    doAnswer(inv -> null).when(consumer).accept(toString());
 
     BiConsumer<Integer, String> biConsumer = mock(BiConsumer.class);
-    doAnswer(inv -> null)
-        .when(biConsumer)
-        .accept(ArgumentMatchers.eq(0), ArgumentMatchers.eq("foo"));
-    doAnswer(inv -> null).when(biConsumer).accept(eq(hashCode()), eq(toString()));
+    doAnswer(inv -> null).when(biConsumer).accept(0, "foo");
+    doAnswer(inv -> null).when(biConsumer).accept(hashCode(), toString());
   }
 }
 

Identification

Shows code lines which will (not) be flagged by this bug pattern.
A //BUG: Diagnostic contains: comment is placed above any violating line.

import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.ArgumentMatchers.notNull;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

import java.util.function.BiConsumer;
import java.util.function.Consumer;
import org.mockito.ArgumentMatchers;

class A {
  void m() {
    Runnable runnable = mock(Runnable.class);
    doAnswer(inv -> null).when(runnable).run();

    Consumer<String> consumer = mock(Consumer.class);
    doAnswer(inv -> null).when(consumer).accept("foo");
    doAnswer(inv -> null).when(consumer).accept(notNull());
    // BUG: Diagnostic contains:
    doAnswer(inv -> null).when(consumer).accept(ArgumentMatchers.eq("foo"));
    // BUG: Diagnostic contains:
    doAnswer(inv -> null).when(consumer).accept(eq(toString()));

    BiConsumer<Integer, String> biConsumer = mock(BiConsumer.class);
    doAnswer(inv -> null).when(biConsumer).accept(0, "foo");
    doAnswer(inv -> null).when(biConsumer).accept(eq(0), notNull());
    doAnswer(inv -> null).when(biConsumer).accept(notNull(), eq("foo"));
    doAnswer(inv -> null)
        .when(biConsumer)
        // BUG: Diagnostic contains:
        .accept(ArgumentMatchers.eq(0), ArgumentMatchers.eq("foo"));
    // BUG: Diagnostic contains:
    doAnswer(inv -> null).when(biConsumer).accept(eq(hashCode()), eq(toString()));
  }
}

Copyright © 2017-2024 Picnic Technologies BV