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

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

Run Mockito automation tests on LambdaTest cloud grid

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

copy
1/*
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;
6
7import static com.google.errorprone.BugPattern.LinkType.CUSTOM;
8import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;
9
10import com.google.auto.service.AutoService;
11import com.google.errorprone.BugPattern;
12import com.google.errorprone.bugpatterns.BugChecker;
13import com.google.errorprone.matchers.Matcher;
14import com.google.errorprone.matchers.Matchers;
15import com.sun.source.tree.ExpressionTree;
16import com.sun.source.tree.MethodInvocationTree;
17import com.sun.tools.javac.code.Type;
18
19/**
20 * Matches on usage of {@code Mockito.argThat(Matcher)} with a matcher that does not extend
21 * ArgumentMatcher.
22 */
23@AutoService(BugChecker.class)
24@BugPattern(
25    name = "MockitoAnyClassWithPrimitiveType",
26    summary = "Matcher inconsistency: use of any(Class) to match a primitive argument",
27    severity = WARNING,
28    linkType = CUSTOM,
29    link = "site.mockito.org/usage/bugpattern/MockitoAnyClassWithPrimitiveType",
30    explanation =
31        "Mockito relies on Java type checking to ensure that parameter matchers are"
32            + " type safe but there are some discrepancies between what the Java type checker"
33            + " allows and what Mockito expects. e.g. Java will allow anyInt() to be used as a"
34            + " matcher for a long parameter because an int can be widened to a long. This"
35            + " checker highlights those incorrect usages and suggests replacements. In Mockito"
36            + " 1.x this was not really an issue because the anyX() methods did not do runtime"
37            + " type checking of the arguments but in Mockito 2.x they do.")
38public class MockitoAnyClassWithPrimitiveType extends AbstractMockitoAnyForPrimitiveType {
39
40  private static final String[] CLASS_NAMES = {
41    "org.mockito.Mockito", "org.mockito.ArgumentMatchers", "org.mockito.Matchers"
42  };
43
44  // Match against the any() or any(Class) methods.
45  private static final Matcher<ExpressionTree> GENERIC_ANY =
46      Matchers.staticMethod().onClassAny(CLASS_NAMES).named("any");
47
48  @Override
49  protected Matcher<? super MethodInvocationTree> matcher() {
50    return GENERIC_ANY;
51  }
52
53  @Override
54  protected String formatMessage(
55      String expectedTypeAsString, Type matcherType, String replacementName) {
56    return String.format(
57        "Matcher mismatch: use %s() matcher to match primitive %s arguments",
58        replacementName, expectedTypeAsString);
59  }
60}
61
Full Screen
copy
1/* (rank 90) copied from https://github.com/mockito/mockito/blob/0ddbcb689e43f6a01f549c5efdd75defb39ebc14/subprojects/errorprone/src/main/java/org/mockito/errorprone/bugpatterns/MockitoAnyIncorrectPrimitiveType.java
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;
6
7import static com.google.errorprone.BugPattern.LinkType.CUSTOM;
8import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;
9import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;
10
11import com.google.auto.service.AutoService;
12import com.google.errorprone.BugPattern;
13import com.google.errorprone.bugpatterns.BugChecker;
14import com.google.errorprone.matchers.Matcher;
15import com.sun.source.tree.ExpressionTree;
16import com.sun.source.tree.MethodInvocationTree;
17import com.sun.tools.javac.code.Type;
18import java.util.regex.Pattern;
19
20/**
21 * Matches on usage of {@code Mockito.argThat(Matcher)} with a matcher that does not extend
22 * ArgumentMatcher.
23 */
24@AutoService(BugChecker.class)
25@BugPattern(
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 = "site.mockito.org/usage/bugpattern/MockitoAnyIncorrectPrimitiveType",
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 {
42
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)?");
46
47  private static final String[] CLASS_NAMES = {
48    "org.mockito.ArgumentMatchers", "org.mockito.Mockito", "org.mockito.Matchers"
49  };
50
51  private static final Matcher<ExpressionTree> METHOD_MATCHER =
52      staticMethod().onClassAny(CLASS_NAMES).withNameMatching(METHOD_NAME_PATTERN).withParameters();
53
54  @Override
55  protected Matcher<? super MethodInvocationTree> matcher() {
56    return METHOD_MATCHER;
57  }
58
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  }
67}
68
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 AbstractMockitoAnyForPrimitiveType

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
LambdaTestX

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
Sarah

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)