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

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

Source:AbstractMockitoAnyForPrimitiveType.java Github

copy

Full Screen

...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

...48 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

...43 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

formatMessage

Using AI Code Generation

copy

Full Screen

1package org.mockito.errorprone.bugpatterns;2import com.google.errorprone.BugPattern;3import com.google.errorprone.BugPattern.SeverityLevel;4import com.google.errorprone.VisitorState;5import com.google.errorprone.bugpatterns.BugChecker;6import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;7import com.google.errorprone.matchers.Description;8import com.google.errorprone.matchers.Matcher;9import com.google.errorprone.matchers.Matchers;10import com.sun.source.tree.MethodInvocationTree;11import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;12import java.util.Objects;13import javax.lang.model.element.Name;14import static com.google.errorprone.BugPattern.Category.JDK;15import static com.google.errorprone.BugPattern.MaturityLevel.EXPERIMENTAL;16import static com.google.errorprone.BugPattern.SeverityLevel.ERROR;17import static com.google.errorprone.matchers.Matchers.instanceMethod;18import static com.google.errorprone.matchers.Matchers.staticMethod;19import static com.sun.source.tree.Tree.Kind.METHOD_INVOCATION;20import static com.sun.source.tree.Tree.Kind.NEW_CLASS;21@BugPattern(22public class AbstractMockitoAnyForPrimitiveType extends BugChecker implements MethodInvocationTreeMatcher {23private static final Matcher<MethodInvocationTree> MATCHER = Matchers.anyOf(24staticMethod().onClass("org.mockito.Matchers").named("anyInt"),25staticMethod().onClass("org.mockito.Matchers").named("anyByte"),26staticMethod().onClass("org.mockito.Matchers").named("anyShort"),27staticMethod().onClass("org.mockito.Matchers").named("anyLong"),28staticMethod().onClass("org.mockito.Matchers").named("anyFloat"),29staticMethod().onClass("org.mockito.Matchers").named("anyDouble"),30staticMethod().onClass("org.mockito.Matchers").named("anyChar"),31staticMethod().onClass("org.mockito.Matchers").named("anyBoolean"),32instanceMethod().onDescendantOf("org.mockito.MockSettings").named("defaultAnswer"),33instanceMethod().onDescendantOf("org.mockito.MockSettings").named("extraInterfaces"),34instanceMethod().onDescendantOf("org.mockito.MockSettings").named("name"),35instanceMethod().onDescendantOf("org.mockito

Full Screen

Full Screen

formatMessage

Using AI Code Generation

copy

Full Screen

1package org.mockito.errorprone.bugpatterns;2import com.google.errorprone.BugPattern;3import com.google.errorprone.BugPattern.Category;4import com.google.errorprone.BugPattern.SeverityLevel;5import com.google.errorprone.VisitorState;6import com.google.errorprone.bugpatterns.BugChecker;7import com.google.errorprone.bugpatterns.BugChecker.MethodInvocationTreeMatcher;8import com.google.errorprone.matchers.Description;9import com.google.errorprone.matchers.Matcher;10import com.google.errorprone.matchers.Matchers;11import com.sun.source.tree.MethodInvocationTree;12import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;13import com.sun.tools.javac.tree.JCTree.JCPrimitiveTypeTree;14import com.sun.tools.javac.tree.JCTree.JCTypeApply;15import com.sun.tools.javac.tree.JCTree.JCTypeCast;16import com.sun.tools.javac.tree.JCTree.JCVariableDecl;17import java.util.List;18import java.util.Optional;19import javax.lang.model.element.Element;20import javax.lang.model.element.ElementKind;21import javax.lang.model.element.Name;22import javax.lang.model.element.VariableElement;23import javax.lang.model.type.TypeKind;24import javax.lang.model.type.TypeMirror;25import javax.lang.model.util.Types;26import org.mockito.errorprone.util.ASTHelpers;27@BugPattern(28 summary = "Prefer Mockito.anyInt() to Mockito.any(int.class)",29 implements MethodInvocationTreeMatcher {30 Matchers.instanceMethod().onDescendantOf("org.mockito.Mockito").named("any");31 public Description matchMethodInvocation(32 MethodInvocationTree tree, VisitorState state) {33 if (!MATCHER.matches(tree, state)) {34 return Description.NO_MATCH;35 }36 ASTHelpers.getSymbol(tree).getParameters();37 if (parameters.isEmpty()) {38 return Description.NO_MATCH;39 }40 VariableElement parameter = parameters.get(0);41 TypeMirror typeMirror = parameter.asType();42 if (typeMirror.getKind().isPrimitive()) {43 return buildDescription(tree).setMessage("message").build();44 }45 return Description.NO_MATCH;46 }

Full Screen

Full Screen

formatMessage

Using AI Code Generation

copy

Full Screen

1import org.mockito.errorprone.bugpatterns.AbstractMockitoAnyForPrimitiveType;2import com.google.errorprone.BugCheckerRefactoringTestHelper;3import com.google.errorprone.refaster.RefactoringValidatorHelper;4import com.google.errorprone.refaster.annotation.AfterTemplate;5import com.google.errorprone.refaster.annotation.BeforeTemplate;6import com.google.errorprone.refaster.annotation.UseImportPolicy;7import org.junit.Test;8import org.junit.runner.RunWith;9import org.junit.runners.JUnit4;10import java.util.List;11import java.util.ArrayList;12import java.util.Arrays;13import java.util.Collections;14import java.util.Comparator;15import java.util.HashSet;16import java.util.Set;17import java.util.TreeSet;18import java.util.function.Function;19import java.util.stream.Collectors;20import java.util.stream.Stream;21import static com.google.errorprone.BugCheckerRefactoringTestHelper.TestMode.TEXT_MATCH;22import static com.google.errorprone.refaster.annotation.UseImportPolicy.STATIC_IMPORT_ALWAYS;23import static org.mockito.Mockito.mock;24import static org.mockito.Mockito.when;25import static org.mockito.Mockito.verify;26import static org.mockito.Mockito.verifyNoMoreInteractions;27import static org.mockito.Mockito.verifyZeroInteractions;28import static org.mockito.Mockito.times;29import static org.mockito.Mockito.anyInt;30import static org.mockito.Mockito.any;31import static org.mockito.Mockito.doThrow;32import static org.mockito.Mockito.doNothing;33import static org.mockito.Mockito.spy;34import static org.mockito.Mockito.never;35import static org.mockito.Mockito.only;36import static org.mockito.Mockito.inOrder;37import static org.mockito.Mockito.ignoreStubs;38import static org.mockito.Mockito.lenient;39import static org.mockito.Mockito.atLeastOnce;40import static org.mockito.Mockito.atLeast;41import static org.mockito.Mockito.atMost;42import static org.mockito.Mockito.atMostOnce;43import static org.mockito.Mockito.after;44import static org.mockito.Mockito.timeout;45import static org.mockito.Mockito.timeo

Full Screen

Full Screen

formatMessage

Using AI Code Generation

copy

Full Screen

1class Test {2 public void test() {3 AbstractMockitoAnyForPrimitiveType abstractMockitoAnyForPrimitiveType = new AbstractMockitoAnyForPrimitiveType();4 abstractMockitoAnyForPrimitiveType.formatMessage("anyString");5 }6}7class Test {8 public void test() {9 AbstractMockitoAnyForPrimitiveType abstractMockitoAnyForPrimitiveType = new AbstractMockitoAnyForPrimitiveType();10 abstractMockitoAnyForPrimitiveType.formatMessage("anyBoolean");11 }12}13class Test {14 public void test() {15 AbstractMockitoAnyForPrimitiveType abstractMockitoAnyForPrimitiveType = new AbstractMockitoAnyForPrimitiveType();16 abstractMockitoAnyForPrimitiveType.formatMessage("anyByte");17 }18}19class Test {20 public void test() {21 AbstractMockitoAnyForPrimitiveType abstractMockitoAnyForPrimitiveType = new AbstractMockitoAnyForPrimitiveType();22 abstractMockitoAnyForPrimitiveType.formatMessage("anyChar");23 }24}25class Test {26 public void test() {27 AbstractMockitoAnyForPrimitiveType abstractMockitoAnyForPrimitiveType = new AbstractMockitoAnyForPrimitiveType();28 abstractMockitoAnyForPrimitiveType.formatMessage("anyDouble");29 }30}31class Test {32 public void test() {33 AbstractMockitoAnyForPrimitiveType abstractMockitoAnyForPrimitiveType = new AbstractMockitoAnyForPrimitiveType();34 abstractMockitoAnyForPrimitiveType.formatMessage("anyFloat");35 }36}

Full Screen

Full Screen

formatMessage

Using AI Code Generation

copy

Full Screen

1package org.mockito.errorprone.bugpatterns;2import java.util.Locale;3import java.util.ResourceBundle;4public class AbstractMockitoAnyForPrimitiveType {5 public void formatMessage() {6 ResourceBundle.getBundle("messages", Locale.ENGLISH).getString("message");7 }8}9[ERROR] /home/dunu008/GSOC/CHECKSTYLE-PROJECT/tests/1.java:7: 'ResourceBundle.getBundle("messages", Locale.ENGLISH).getString("message")' should be replaced with 'anyString()'. [AbstractMockitoAnyForPrimitiveType]

Full Screen

Full Screen

formatMessage

Using AI Code Generation

copy

Full Screen

1import java.io.*;2import java.util.*;3import java.lang.reflect.*;4import java.util.regex.*;5import java.util.stream.Collectors;6import java.util.stream.Stream;7import com.sun.tools.javac.tree.JCTree.*;8import com.sun.source.tree.*;9import com.sun.source.util.*;10import com.sun.source.tree.Tree.Kind;11import com.sun.source.util.Trees;12import com.sun.source.util.TreePathScanner;13import com.sun.source.util.TreePath;14import com.sun.tools.javac.api.JavacTrees;15import com.sun.tools.javac.tree.JCTree.*;16import com.sun.tools.javac.tree.JCTree.JCMethodInvocation;17import com.sun.tools.javac.tree.JCTree.JCExpression;18import com.sun.tools.javac.tree.JCTree.JCMethodDecl;19import com.sun.tools.javac.tree.JCTree.JCVariableDecl;20import com.sun.tools.javac.tree.JCTree.JCClassDecl;21import com.sun.tools.javac.tree.JCTree.JCStatement;22import com.sun.tools.javac.tree.JCTree.JCBlock;23import com.sun.tools.javac.tree.JCTree.JCExpressionStatement;24import com.sun.tools.javac.tree.JCTree.JCReturn;25import com.sun.tools.javac.tree.JCTree.JCIdent;26import com.sun.tools.javac.tree.JCTree.JCLiteral;27import com.sun.tools.javac.tree.JCTree.JCNewClass;28import com.sun.tools.javac.tree.JCTree.JCAssign;29import com.sun.tools.javac.tree.JCTree.JCIf;30import com.sun.tools.javac.tree.JCTree.JCForLoop;31import com.sun.tools.javac.tree.JCTree.JCEnhancedForLoop;32import com.sun.tools.javac.tree.JCTree.JCDoWhileLoop;33import com.sun.tools.javac.tree.JCTree.JCWhileLoop;34import com.sun.tools.javac.tree.JCTree.JCSwitch;35import com.sun.tools.javac.tree.JCTree.JCCase;36import com.sun.tools.javac.tree.JCTree.JCBreak;37import com.sun.tools.javac.tree.JCTree.JCContinue;38import com.sun.tools.javac.tree.JCTree.JCThrow;39import com.sun.tools.javac.tree.JCTree.JCTry;40import com.sun.tools.javac.tree.JCTree.J

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