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

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

Source:AbstractMockitoAnyForPrimitiveType.java Github

copy

Full Screen

2 * Copyright (c) 2017 Mockito contributors3 * This program is made available under the terms of the MIT License.4 */5package org.mockito.errorprone.bugpatterns;6import static com.google.errorprone.matchers.Description.NO_MATCH;7import com.google.errorprone.VisitorState;8import com.google.errorprone.bugpatterns.BugChecker;9import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;10import com.google.errorprone.fixes.SuggestedFix;11import com.google.errorprone.matchers.Description;12import com.google.errorprone.matchers.Matcher;13import com.google.errorprone.util.ASTHelpers;14import com.sun.source.tree.ExpressionTree;15import com.sun.source.tree.MemberSelectTree;16import com.sun.source.tree.MethodInvocationTree;17import com.sun.source.tree.Tree;18import com.sun.source.util.TreePath;19import com.sun.tools.javac.code.Symbol.MethodSymbol;20import com.sun.tools.javac.code.Symbol.VarSymbol;21import com.sun.tools.javac.code.Type;22import com.sun.tools.javac.code.Type.ArrayType;23import java.util.List;24import javax.lang.model.type.TypeKind;25/** Base for {@link BugChecker}s that detect issues with any() matchers and primitive types. */26public abstract class AbstractMockitoAnyForPrimitiveType extends BugChecker27 implements MethodInvocationTreeMatcher {28 protected abstract Matcher<? super MethodInvocationTree> matcher();29 protected abstract String formatMessage(30 String expectedTypeAsString, Type matcherType, String replacementName);31 @Override32 public Description matchMethodInvocation(MethodInvocationTree tree, VisitorState state) {33 if (!matcher().matches(tree, state)) {34 return NO_MATCH;35 }36 MethodSymbol method = ASTHelpers.getSymbol(tree);37 Type matcherType = method.getReturnType();38 // It is expected that the call to anyX() is itself the argument to another call which is39 // the one being mocked, e.g. something like this:40 // when(mock.call(..., anyInt(), ...))...41 TreePath path = state.getPath();42 Tree parentTree = path.getParentPath().getLeaf();43 if (!(parentTree instanceof MethodInvocationTree)) {44 // Ignore calls that are not arguments to another method call.45 // TODO: Report this as a problem because it makes little sense.46 // TODO: Support casting.47 return NO_MATCH;48 }49 MethodInvocationTree parentCall = (MethodInvocationTree) parentTree;50 MethodSymbol parentMethod = ASTHelpers.getSymbol(parentCall);51 // Find the index of the argument in the parent call.52 int argumentIndex = -1;53 List<? extends ExpressionTree> parentArguments = parentCall.getArguments();54 for (int i = 0; i < parentArguments.size(); i++) {55 ExpressionTree argumentTree = parentArguments.get(i);56 if (argumentTree == tree) {57 argumentIndex = i;58 break;59 }60 }61 if (argumentIndex == -1) {62 throw new IllegalStateException(63 "Cannot find argument " + tree + " in argument list from " + parentTree);64 }65 Type parameterType = getParameterType(parentMethod, argumentIndex);66 TypeKind parameterTypeKind = parameterType.getKind();67 if (parameterTypeKind.isPrimitive() && parameterTypeKind != matcherType.getKind()) {68 String expectedTypeAsString = parameterType.toString();69 String replacementName =70 "any"71 + Character.toUpperCase(expectedTypeAsString.charAt(0))72 + expectedTypeAsString.substring(1);73 String message = formatMessage(expectedTypeAsString, matcherType, replacementName);74 SuggestedFix.Builder fixBuilder = SuggestedFix.builder();75 ExpressionTree methodSelect = tree.getMethodSelect();76 String replacement;77 if (methodSelect instanceof MemberSelectTree) {78 MemberSelectTree qualifier = (MemberSelectTree) methodSelect;79 replacement = state.getSourceForNode(qualifier.getExpression()) + "." + replacementName;80 } else {81 replacement = replacementName;82 String staticImport = method.owner + "." + replacementName;83 fixBuilder.addStaticImport(staticImport);84 }85 SuggestedFix fix = fixBuilder.replace(tree, replacement + "()").build();86 return buildDescription(tree).setMessage(message).addFix(fix).build();87 }...

Full Screen

Full Screen

Source:MockitoAnyIncorrectPrimitiveType.java Github

copy

Full Screen

...4 */5package org.mockito.errorprone.bugpatterns;6import static com.google.errorprone.BugPattern.LinkType.CUSTOM;7import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;8import static com.google.errorprone.matchers.method.MethodMatchers.staticMethod;9import com.google.auto.service.AutoService;10import com.google.errorprone.BugPattern;11import com.google.errorprone.bugpatterns.BugChecker;12import com.google.errorprone.matchers.Matcher;13import com.sun.source.tree.ExpressionTree;14import com.sun.source.tree.MethodInvocationTree;15import com.sun.tools.javac.code.Type;16import java.util.regex.Pattern;17/**18 * Matches on usage of {@code Mockito.argThat(Matcher)} with a matcher that does not extend19 * ArgumentMatcher.20 */21@AutoService(BugChecker.class)22@BugPattern(23 name = "MockitoAnyIncorrectPrimitiveType",24 summary = "Matcher mismatch: incorrect use of any() or anyX() to match a primitive argument",25 severity = ERROR,26 linkType = CUSTOM,27 link = "site.mockito.org/usage/bugpattern/MockitoAnyIncorrectPrimitiveType",28 explanation =29 "Mockito relies on Java type checking to ensure that parameter matchers are"30 + " type safe but there are some discrepancies between what the Java type checker"31 + " allows and what Mockito expects. e.g. Java will allow anyInt() to be used as a"32 + " matcher for a long parameter because an int can be widened to a long. This"33 + " checker highlights those incorrect usages and suggests replacements. In Mockito"34 + " 1.x this was not really an issue because the anyX() methods did not do runtime"35 + " type checking of the arguments but in Mockito 2.x they do."36 + " Java will also allow any() to be used within a primitive but any() returns null and"37 + " the compiler wraps that call in unboxing which leads to a NPE.")38public class MockitoAnyIncorrectPrimitiveType extends AbstractMockitoAnyForPrimitiveType {39 // Match against any() or any of the any<x>() methods.40 private static final Pattern METHOD_NAME_PATTERN =41 Pattern.compile("any(Boolean|Byte|Char|Double|Float|Int|Long|Short)?");42 private static final String[] CLASS_NAMES = {43 "org.mockito.ArgumentMatchers", "org.mockito.Mockito", "org.mockito.Matchers"44 };45 private static final Matcher<ExpressionTree> METHOD_MATCHER =46 staticMethod().onClassAny(CLASS_NAMES).withNameMatching(METHOD_NAME_PATTERN).withParameters();47 @Override48 protected Matcher<? super MethodInvocationTree> matcher() {49 return METHOD_MATCHER;50 }51 @Override52 protected String formatMessage(53 String expectedTypeAsString, Type matcherType, String replacementName) {54 return String.format(55 "Matcher mismatch: expected matcher for parameter of type '%s',"56 + " found matcher for parameter of type '%s'",57 expectedTypeAsString, matcherType);58 }59}...

Full Screen

Full Screen

Source:MockitoAnyClassWithPrimitiveType.java Github

copy

Full Screen

...7import static com.google.errorprone.BugPattern.SeverityLevel.WARNING;8import com.google.auto.service.AutoService;9import com.google.errorprone.BugPattern;10import com.google.errorprone.bugpatterns.BugChecker;11import com.google.errorprone.matchers.Matcher;12import com.google.errorprone.matchers.Matchers;13import com.sun.source.tree.ExpressionTree;14import com.sun.source.tree.MethodInvocationTree;15import com.sun.tools.javac.code.Type;16/**17 * Matches on usage of {@code Mockito.argThat(Matcher)} with a matcher that does not extend18 * ArgumentMatcher.19 */20@AutoService(BugChecker.class)21@BugPattern(22 name = "MockitoAnyClassWithPrimitiveType",23 summary = "Matcher inconsistency: use of any(Class) to match a primitive argument",24 severity = WARNING,25 linkType = CUSTOM,26 link = "site.mockito.org/usage/bugpattern/MockitoAnyClassWithPrimitiveType",27 explanation =28 "Mockito relies on Java type checking to ensure that parameter matchers are"29 + " type safe but there are some discrepancies between what the Java type checker"30 + " allows and what Mockito expects. e.g. Java will allow anyInt() to be used as a"31 + " matcher for a long parameter because an int can be widened to a long. This"32 + " checker highlights those incorrect usages and suggests replacements. In Mockito"33 + " 1.x this was not really an issue because the anyX() methods did not do runtime"34 + " type checking of the arguments but in Mockito 2.x they do.")35public class MockitoAnyClassWithPrimitiveType extends AbstractMockitoAnyForPrimitiveType {36 private static final String[] CLASS_NAMES = {37 "org.mockito.Mockito", "org.mockito.ArgumentMatchers", "org.mockito.Matchers"38 };39 // Match against the any() or any(Class) methods.40 private static final Matcher<ExpressionTree> GENERIC_ANY =41 Matchers.staticMethod().onClassAny(CLASS_NAMES).named("any");42 @Override43 protected Matcher<? super MethodInvocationTree> matcher() {44 return GENERIC_ANY;45 }46 @Override47 protected String formatMessage(48 String expectedTypeAsString, Type matcherType, String replacementName) {49 return String.format(50 "Matcher mismatch: use %s() matcher to match primitive %s arguments",51 replacementName, expectedTypeAsString);52 }53}...

Full Screen

Full Screen

matcher

Using AI Code Generation

copy

Full Screen

1import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;2import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType.Matcher;3import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType.MatchResult;4public class Test {5 public void test() {6 Matcher matcher = new AbstractMockitoAnyForPrimitiveType.Matcher();7 MatchResult matchResult = matcher.matchMethodInvocation(null, null);8 }9}10import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;11import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType.Matcher;12import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType.MatchResult;13public class Test {14 public void test() {15 Matcher matcher = new AbstractMockitoAnyForPrimitiveType.Matcher();16 MatchResult matchResult = matcher.matchMethodInvocation(null, null);17 }18}19 MatchResult matchResult = matcher.matchMethodInvocation(null, null);20 symbol: method matchMethodInvocation(null,null)21 MatchResult matchResult = matcher.matchMethodInvocation(null, null);22 symbol: method matchMethodInvocation(null,null)23public class TestClass {24 public void testMethod() {25 }26}27public class TestClassTest {28 public void testMethod() {29 TestClass testClass = new TestClass();30 testClass.testMethod();31 }32}33public class TestClass {34 public void testMethod() {35 try {36 } catch (Exception e) {37 }38 }39}

Full Screen

Full Screen

matcher

Using AI Code Generation

copy

Full Screen

1import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;2import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType.Matcher;3import java.util.regex.Pattern;4import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType.Matcher;5public class Test {6 public static void main(String[] args) {7 Matcher matcher = AbstractMockitoAnyForPrimitiveType.matcher("anyInt");8 System.out.println(matcher.matches());9 }10}11import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;12import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType.Matcher;13import java.util.regex.Pattern;14import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType.Matcher;15public class Test {16 public static void main(String[] args) {17 Matcher matcher = AbstractMockitoAnyForPrimitiveType.matcher("anyInt");18 System.out.println(matcher.matches());19 }20}

Full Screen

Full Screen

matcher

Using AI Code Generation

copy

Full Screen

1import static org.mockito.Mockito.*;2import org.mockito.Mock;3import org.mockito.Mockito;4import org.mockito.MockitoAnnotations;5import org.mockito.internal.matchers.Any;6import org.mockito.internal.matchers.AnyVararg;7import org.mockito.invocation.InvocationOnMock;8import org.mockito.stubbing.Answer;9import org.mockito.stubbing.OngoingStubbing;10import org.mockito.stubbing.Stubber;11import org.mockito.stubbing.VoidMethodStubbable;12import org.mockito.verification.VerificationMode;13import org.mockito.verification.VerificationWithTimeout;14import org.mockito.internal.verification.VerificationModeFactory;15import org.mockito.exceptions.verification.NoInteractionsWanted;16import org.mockito.exceptions.verification.TooLittleActualInvocations;17import org.mockito.exceptions.verification.TooManyActualInvocations;18import org.mockito.exceptions.verification.WantedButNotInvoked;19import org.mockito.exceptions.verification.junit.ArgumentsAreDifferent;20import org.mockito.exceptions.verification.junit.JUnitNoInteractionsWanted;21import org.mockito.exceptions.verification.junit.JUnitTooLittleActualInvocations;22import org.mockito.exceptions.verification.junit.JUnitTooManyActualInvocations;23import org.mockito.exceptions.verification.junit.JUnitWantedButNotInvoked;24import org.mockito.exceptions.verification.junit.WantedButNotInvokedInOrder;25import org.mockito.exceptions.verification.junit.JUnitWantedButNotInvokedInOr

Full Screen

Full Screen

matcher

Using AI Code Generation

copy

Full Screen

1package org.mockito.errorprone.bugpatterns;2import static org.mockito.Mockito.mock;3import java.util.List;4public class Test {5 public static void main(String[] args) {6 List<String> mockedList = mock(List.class);7 mockedList.add("one");8 mockedList.add("two");9 mockedList.add("three");10 mockedList.add("four");11 mockedList.add("five");12 mockedList.add("six");13 mockedList.add("seven");14 mockedList.add("eight");15 mockedList.add("nine");16 mockedList.add("ten");17 mockedList.add("eleven");18 mockedList.add("twelve");19 mockedList.add("thirteen");20 mockedList.add("fourteen");21 mockedList.add("fifteen");22 mockedList.add("sixteen");23 mockedList.add("seventeen");24 mockedList.add("eighteen");25 mockedList.add("nineteen");26 mockedList.add("twenty");27 mockedList.add("twentyone");28 mockedList.add("twentytwo");29 mockedList.add("twentythree");30 mockedList.add("twentyfour");31 mockedList.add("twentyfive");32 mockedList.add("twentysix");33 mockedList.add("twentyseven");34 mockedList.add("twentyeight");35 mockedList.add("twentynine");36 mockedList.add("thirty");37 mockedList.add("thirtyone");38 mockedList.add("thirtytwo");39 mockedList.add("thirtythree");40 mockedList.add("thirtyfour");41 mockedList.add("thirtyfive");42 mockedList.add("thirtysix");43 mockedList.add("thirtyseven");44 mockedList.add("thirtyeight");45 mockedList.add("thirtynine");46 mockedList.add("forty");47 mockedList.add("fortyone");48 mockedList.add("fortytwo");49 mockedList.add("fortythree");50 mockedList.add("fortyfour");51 mockedList.add("fortyfive");52 mockedList.add("fortysix");53 mockedList.add("fortyseven");54 mockedList.add("fortyeight");55 mockedList.add("fortynine");56 mockedList.add("fifty");57 mockedList.add("fiftyone");58 mockedList.add("fiftytwo");59 mockedList.add("fiftythree");60 mockedList.add("fiftyfour

Full Screen

Full Screen

matcher

Using AI Code Generation

copy

Full Screen

1import static org.mockito.Mockito.*;2public class 1 {3 public static void main(String[] args) {4 AbstractMockitoAnyForPrimitiveType mock = mock(AbstractMockitoAnyForPrimitiveType.class);5 when(mock.matcher()).thenReturn(1);6 }7}8import static org.mockito.Mockito.*;9public class 2 {10 public static void main(String[] args) {11 AbstractMockitoAnyForPrimitiveType mock = mock(AbstractMockitoAnyForPrimitiveType.class);12 when(mock.matcher()).thenReturn(1);13 }14}15import static org.mockito.Mockito.*;16public class 3 {17 public static void main(String[] args) {18 AbstractMockitoAnyForPrimitiveType mock = mock(AbstractMockitoAnyForPrimitiveType.class);19 when(mock.matcher()).thenReturn(1);20 }21}22import static org.mockito.Mockito.*;23public class 4 {24 public static void main(String[] args) {25 AbstractMockitoAnyForPrimitiveType mock = mock(AbstractMockitoAnyForPrimitiveType.class);26 when(mock.matcher()).thenReturn(1);27 }28}29import static org.mockito.Mockito.*;30public class 5 {31 public static void main(String[] args) {32 AbstractMockitoAnyForPrimitiveType mock = mock(AbstractMockitoAnyForPrimitiveType.class);33 when(mock.matcher()).thenReturn(1);34 }35}

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Mockito automation tests on LambdaTest cloud grid

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

Most used method in AbstractMockitoAnyForPrimitiveType

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful