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

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

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

matcher

Using AI Code Generation

copy

Full Screen

1import com.google.errorprone.CompilationTestHelper;2import com.google.errorprone.bugpatterns.BugCheckerRefactoringTestHelper;3import org.junit.Test;4import org.junit.runner.RunWith;5import org.junit.runners.JUnit4;6@RunWith(JUnit4.class)7public class MockitoAnyIncorrectPrimitiveTypeTest {8 BugCheckerRefactoringTestHelper.newInstance(9 new MockitoAnyIncorrectPrimitiveType(), getClass());10 CompilationTestHelper.newInstance(MockitoAnyIncorrectPrimitiveType.class, getClass());11 public void positiveCases() {12 compilationHelper.addSourceFile("MockitoAnyIncorrectPrimitiveTypePositiveCases.java").doTest();13 }14 public void negativeCases() {15 compilationHelper.addSourceFile("MockitoAnyIncorrectPrimitiveTypeNegativeCases.java").doTest();16 }17 public void testRefactoring() {18 .addInputLines(19 "import static org.mockito.ArgumentMatchers.anyInt;",20 "import static org.mockito.Mockito.when;",21 "class Test {",22 " void f() {",23 " when(1).thenReturn(1);",24 " }",25 "}")26 .addOutputLines(27 "import static org.mockito.ArgumentMatchers.anyInt;",28 "import static org.mockito.Mockito.when;",29 "class Test {",30 " void f() {",31 " when(anyInt()).thenReturn(1);",32 " }",33 "}")34 .doTest();35 }36}

Full Screen

Full Screen

matcher

Using AI Code Generation

copy

Full Screen

1import org.mockito.errorprone.bugpatterns.MockitoAnyIncorrectPrimitiveType;2class MockitoAnyIncorrectPrimitiveTypePositiveCases {3 void testInt() {4 Mockito.anyInt();5 }6 void testLong() {7 Mockito.anyLong();8 }9 void testDouble() {10 Mockito.anyDouble();11 }12 void testFloat() {13 Mockito.anyFloat();14 }15 void testByte() {16 Mockito.anyByte();17 }18 void testShort() {19 Mockito.anyShort();20 }21 void testChar() {22 Mockito.anyChar();23 }24 void testBoolean() {25 Mockito.anyBoolean();26 }27}28class MockitoAnyIncorrectPrimitiveTypeNegativeCases {29 void testInt() {30 Mockito.any(Integer.class);31 }32 void testLong() {33 Mockito.any(Long.class);34 }35 void testDouble() {36 Mockito.any(Double.class);37 }38 void testFloat() {39 Mockito.any(Float.class);40 }41 void testByte() {42 Mockito.any(Byte.class);43 }44 void testShort() {45 Mockito.any(Short.class);46 }47 void testChar() {48 Mockito.any(Character.class);49 }50 void testBoolean() {51 Mockito.any(Boolean.class);52 }53}

Full Screen

Full Screen

matcher

Using AI Code Generation

copy

Full Screen

1public class MockitoAnyIncorrectPrimitiveType {2 public boolean matcher(String type) {3 if (type.equals("boolean") || type.equals("char") || type.equals("byte") || type.equals("short") || type.equals("int") || type.equals("long") || type.equals("float") || type.equals("double")) {4 return true;5 } else {6 return false;7 }8 }9}10import org.junit.Test;11import static org.junit.Assert.*;12public class MockitoAnyIncorrectPrimitiveTypeTest {13 public void testMatcher() {14 MockitoAnyIncorrectPrimitiveType mockitoAnyIncorrectPrimitiveType = new MockitoAnyIncorrectPrimitiveType();15 assertTrue(mockitoAnyIncorrectPrimitiveType.matcher("boolean"));16 assertFalse(mockitoAnyIncorrectPrimitiveType.matcher("String"));17 }18}

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 MockitoAnyIncorrectPrimitiveType

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful