How to use method of org.mockito.errorprone.bugpatterns.MockitoAnyIncorrectPrimitiveType class

Best Mockito code snippet using org.mockito.errorprone.bugpatterns.MockitoAnyIncorrectPrimitiveType.

Run Mockito automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

1/* (rank 90) copied from
2 * Copyright (c) 2017 Mockito contributors
3 * This program is made available under the terms of the MIT License.
4 */
5package org.mockito.errorprone.bugpatterns;
7import static;
8import static;
9import static;
15import com.sun.source.tree.ExpressionTree;
16import com.sun.source.tree.MethodInvocationTree;
18import java.util.regex.Pattern;
21 * Matches on usage of {@code Mockito.argThat(Matcher)} with a matcher that does not extend
22 * ArgumentMatcher.
23 */
26    name = "MockitoAnyIncorrectPrimitiveType",
27    summary = "Matcher mismatch: incorrect use of any() or anyX() to match a primitive argument",
28    severity = ERROR,
29    linkType = CUSTOM,
30    link = "",
31    explanation =
32        "Mockito relies on Java type checking to ensure that parameter matchers are"
33            + " type safe but there are some discrepancies between what the Java type checker"
34            + " allows and what Mockito expects. e.g. Java will allow anyInt() to be used as a"
35            + " matcher for a long parameter because an int can be widened to a long. This"
36            + " checker highlights those incorrect usages and suggests replacements. In Mockito"
37            + " 1.x this was not really an issue because the anyX() methods did not do runtime"
38            + " type checking of the arguments but in Mockito 2.x they do."
39            + " Java will also allow any() to be used within a primitive but any() returns null and"
40            + " the compiler wraps that call in unboxing which leads to a NPE.")
41public class MockitoAnyIncorrectPrimitiveType extends AbstractMockitoAnyForPrimitiveType {
43  // Match against any() or any of the any<x>() methods.
44  private static final Pattern METHOD_NAME_PATTERN =
45      Pattern.compile("any(Boolean|Byte|Char|Double|Float|Int|Long|Short)?");
47  private static final String[] CLASS_NAMES = {
48    "org.mockito.ArgumentMatchers", "org.mockito.Mockito", "org.mockito.Matchers"
49  };
51  private static final Matcher<ExpressionTree> METHOD_MATCHER =
52      staticMethod().onClassAny(CLASS_NAMES).withNameMatching(METHOD_NAME_PATTERN).withParameters();
54  @Override
55  protected Matcher<? super MethodInvocationTree> matcher() {
56    return METHOD_MATCHER;
57  }
59  @Override
60  protected String formatMessage(
61      String expectedTypeAsString, Type matcherType, String replacementName) {
62    return String.format(
63        "Matcher mismatch: expected matcher for parameter of type '%s',"
64            + " found matcher for parameter of type '%s'",
65        expectedTypeAsString, matcherType);
66  }
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Most used method in MockitoAnyIncorrectPrimitiveType

Trigger code on LambdaTest Cloud Grid

Execute automation tests with on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)