Best Mockito code snippet using org.mockito.internal.matchers.LocalizedMatcher.LocalizedMatcher
Source:MockitoApi.java
...21import java.util.List;22import org.hamcrest.Matcher;23import org.mockito.Answers;24import org.mockito.internal.InternalMockHandler;25import org.mockito.internal.matchers.LocalizedMatcher;26import org.mockito.internal.progress.ArgumentMatcherStorage;27import org.mockito.internal.progress.MockingProgress;28import org.mockito.internal.progress.ThreadSafeMockingProgress;29import org.mockito.internal.stubbing.InvocationContainer;30import org.mockito.internal.util.MockUtil;31import org.mockito.internal.verification.MockAwareVerificationMode;32import org.mockito.mock.MockCreationSettings;33import org.mockito.stubbing.Answer;34import org.mockito.verification.VerificationMode;35import org.springframework.beans.BeanUtils;36import org.springframework.util.ClassUtils;37import org.springframework.util.ReflectionUtils;38/**39 * A facade for Mockito APIs that have changed between Mockito 1 and Mockito 2.40 *41 * @author Andy Wilkinson42 * @author Stephane Nicoll43 * @author Phillip Webb44 */45abstract class MockitoApi {46 private static final MockitoApi api = createApi();47 /**48 * Return mock settings for the given mock object.49 * @param mock the mock object50 * @return the mock creation settings51 */52 public abstract MockCreationSettings<?> getMockSettings(Object mock);53 /**54 * Return the mocking progress for the current thread.55 * @param mock the mock object56 * @return the current mocking progress57 */58 public abstract MockingProgress mockingProgress(Object mock);59 /**60 * Set report matchers to the given storage.61 * @param storage the storage to use62 * @param matchers the matchers to set63 */64 public abstract void reportMatchers(ArgumentMatcherStorage storage,65 List<LocalizedMatcher> matchers);66 /**67 * Create a new {@link MockAwareVerificationMode} instance.68 * @param mock the source mock69 * @param mode the verification mode70 * @return a new {@link MockAwareVerificationMode} instance71 */72 public abstract MockAwareVerificationMode createMockAwareVerificationMode(Object mock,73 VerificationMode mode);74 /**75 * Return the {@link Answer} for a given {@link Answers} value.76 * @param answer the source answers77 * @return the answer78 */79 public abstract Answer<Object> getAnswer(Answers answer);80 /**81 * Factory to create the appropriate API version.82 * @return the API version83 */84 private static MockitoApi createApi() {85 if (ClassUtils.isPresent("org.mockito.ReturnValues", null)) {86 return new Mockito1Api();87 }88 return new Mockito2Api();89 }90 /**91 * Get the API for the running mockito version.92 * @return the API93 */94 public static MockitoApi get() {95 return api;96 }97 /**98 * {@link MockitoApi} for Mockito 1.0.99 */100 private static class Mockito1Api extends MockitoApi {101 private final MockUtil mockUtil;102 private final Method getMockSettingsMethod;103 private final Method getMockHandlerMethod;104 private Method reportMatcherMethod;105 private Constructor<MockAwareVerificationMode> mockAwareVerificationModeConstructor;106 Mockito1Api() {107 this.mockUtil = BeanUtils.instantiateClass(MockUtil.class);108 this.getMockSettingsMethod = ReflectionUtils.findMethod(MockUtil.class,109 "getMockSettings", Object.class);110 this.getMockHandlerMethod = ReflectionUtils.findMethod(MockUtil.class,111 "getMockHandler", Object.class);112 this.reportMatcherMethod = ReflectionUtils.findMethod(113 ArgumentMatcherStorage.class, "reportMatcher", Matcher.class);114 this.mockAwareVerificationModeConstructor = ClassUtils115 .getConstructorIfAvailable(MockAwareVerificationMode.class,116 Object.class, VerificationMode.class);117 }118 @Override119 public MockCreationSettings<?> getMockSettings(Object mock) {120 return (MockCreationSettings<?>) ReflectionUtils121 .invokeMethod(this.getMockSettingsMethod, this.mockUtil, mock);122 }123 @Override124 public MockingProgress mockingProgress(Object mock) {125 InternalMockHandler<?> handler = (InternalMockHandler<?>) ReflectionUtils126 .invokeMethod(this.getMockHandlerMethod, this.mockUtil, mock);127 InvocationContainer container = handler.getInvocationContainer();128 Field field = ReflectionUtils.findField(container.getClass(),129 "mockingProgress");130 ReflectionUtils.makeAccessible(field);131 return (MockingProgress) ReflectionUtils.getField(field, container);132 }133 @Override134 public void reportMatchers(ArgumentMatcherStorage storage,135 List<LocalizedMatcher> matchers) {136 for (LocalizedMatcher matcher : matchers) {137 ReflectionUtils.invokeMethod(this.reportMatcherMethod, storage, matcher);138 }139 }140 @Override141 public MockAwareVerificationMode createMockAwareVerificationMode(Object mock,142 VerificationMode mode) {143 return BeanUtils.instantiateClass(this.mockAwareVerificationModeConstructor,144 mock, mode);145 }146 @Override147 @SuppressWarnings("deprecation")148 public Answer<Object> getAnswer(Answers answer) {149 return answer.get();150 }151 }152 /**153 * {@link MockitoApi} for Mockito 2.0.154 */155 private static class Mockito2Api extends MockitoApi {156 @Override157 public MockCreationSettings<?> getMockSettings(Object mock) {158 return MockUtil.getMockSettings(mock);159 }160 @Override161 public MockingProgress mockingProgress(Object mock) {162 return ThreadSafeMockingProgress.mockingProgress();163 }164 @Override165 public void reportMatchers(ArgumentMatcherStorage storage,166 List<LocalizedMatcher> matchers) {167 for (LocalizedMatcher matcher : matchers) {168 storage.reportMatcher(matcher.getMatcher());169 }170 }171 @Override172 public MockAwareVerificationMode createMockAwareVerificationMode(Object mock,173 VerificationMode mode) {174 try {175 return new MockAwareVerificationMode(mock, mode, Collections.emptySet());176 }177 catch (NoSuchMethodError ex) {178 // Earlier versions of 2.x did not have the collection parameter179 Constructor<MockAwareVerificationMode> constructor = ClassUtils180 .getConstructorIfAvailable(MockAwareVerificationMode.class,181 Object.class, VerificationMode.class);...
Source:SpringBootMockUtil.java
...16package org.springframework.boot.test.mock.mockito;17import java.lang.reflect.Method;18import java.util.List;19import org.mockito.ArgumentMatcher;20import org.mockito.internal.matchers.LocalizedMatcher;21import org.mockito.internal.progress.ArgumentMatcherStorage;22import org.mockito.internal.progress.MockingProgress;23import org.mockito.internal.progress.ThreadSafeMockingProgress;24import org.mockito.internal.util.MockUtil;25import org.mockito.mock.MockCreationSettings;26import org.springframework.util.ClassUtils;27import org.springframework.util.ReflectionUtils;28/**29 * A facade for Mockito's {@link MockUtil} that hides API differences between Mockito 130 * and 2.31 *32 * @author Andy Wilkinson33 */34final class SpringBootMockUtil {35 private static final MockUtilAdapter adapter;36 static {37 if (ClassUtils.isPresent("org.mockito.quality.MockitoHint",38 SpringBootMockUtil.class.getClassLoader())) {39 adapter = new Mockito2MockUtilAdapter();40 }41 else {42 adapter = new Mockito1MockUtilAdapter();43 }44 }45 private SpringBootMockUtil() {46 }47 static MockCreationSettings<?> getMockSettings(Object mock) {48 return adapter.getMockSettings(mock);49 }50 static MockingProgress mockingProgress() {51 return adapter.mockingProgress();52 }53 static void reportMatchers(ArgumentMatcherStorage storage,54 List<LocalizedMatcher> matchers) {55 adapter.reportMatchers(storage, matchers);56 }57 private interface MockUtilAdapter {58 MockCreationSettings<?> getMockSettings(Object mock);59 MockingProgress mockingProgress();60 void reportMatchers(ArgumentMatcherStorage storage,61 List<LocalizedMatcher> matchers);62 }63 private static class Mockito1MockUtilAdapter implements MockUtilAdapter {64 private static final MockingProgress mockingProgress = new ThreadSafeMockingProgress();65 @Override66 public MockCreationSettings<?> getMockSettings(Object mock) {67 return new MockUtil().getMockSettings(mock);68 }69 @Override70 public MockingProgress mockingProgress() {71 return mockingProgress;72 }73 @Override74 public void reportMatchers(ArgumentMatcherStorage storage,75 List<LocalizedMatcher> matchers) {76 for (LocalizedMatcher matcher : matchers) {77 storage.reportMatcher(matcher);78 }79 }80 }81 private static class Mockito2MockUtilAdapter implements MockUtilAdapter {82 private final Method getMockSettingsMethod = ReflectionUtils83 .findMethod(MockUtil.class, "getMockSettings", Object.class);84 private final Method mockingProgressMethod = ReflectionUtils85 .findMethod(ThreadSafeMockingProgress.class, "mockingProgress");86 private final Method reportMatcherMethod = ReflectionUtils.findMethod(87 ArgumentMatcherStorage.class, "reportMatcher", ArgumentMatcher.class);88 private final Method getMatcherMethod = ReflectionUtils89 .findMethod(LocalizedMatcher.class, "getMatcher");90 @Override91 public MockCreationSettings<?> getMockSettings(Object mock) {92 return (MockCreationSettings<?>) ReflectionUtils93 .invokeMethod(this.getMockSettingsMethod, null, mock);94 }95 @Override96 public MockingProgress mockingProgress() {97 return (MockingProgress) ReflectionUtils98 .invokeMethod(this.mockingProgressMethod, null);99 }100 @Override101 public void reportMatchers(ArgumentMatcherStorage storage,102 List<LocalizedMatcher> matchers) {103 for (LocalizedMatcher matcher : matchers) {104 ReflectionUtils.invokeMethod(this.reportMatcherMethod, storage,105 ReflectionUtils.invokeMethod(this.getMatcherMethod, matcher));106 }107 }108 }109}...
Source:ArgumentMatcherStorageImpl.java
...11import org.hamcrest.Matcher;12import org.mockito.exceptions.Reporter;13import org.mockito.exceptions.misusing.InvalidUseOfMatchersException;14import org.mockito.internal.matchers.And;15import org.mockito.internal.matchers.LocalizedMatcher;16import org.mockito.internal.matchers.Not;17import org.mockito.internal.matchers.Or;18@SuppressWarnings("unchecked")19public class ArgumentMatcherStorageImpl implements ArgumentMatcherStorage {20 21 private Stack<LocalizedMatcher> matcherStack = new Stack<LocalizedMatcher>();22 23 /* (non-Javadoc)24 * @see org.mockito.internal.progress.ArgumentMatcherStorage#reportMatcher(org.hamcrest.Matcher)25 */26 public HandyReturnValues reportMatcher(Matcher matcher) {27 matcherStack.push(new LocalizedMatcher(matcher));28 return new HandyReturnValues();29 }30 /* (non-Javadoc)31 * @see org.mockito.internal.progress.ArgumentMatcherStorage#pullMatchers()32 */33 public List<Matcher> pullMatchers() {34 if (matcherStack.isEmpty()) {35 return Collections.emptyList();36 }37 38 List<LocalizedMatcher> matchers = new ArrayList<LocalizedMatcher>(matcherStack);39 matcherStack.clear();40 return (List) matchers;41 }42 /* (non-Javadoc)43 * @see org.mockito.internal.progress.ArgumentMatcherStorage#reportAnd()44 */45 public HandyReturnValues reportAnd() {46 assertState(!matcherStack.isEmpty(), "No matchers found for And(?).");47 And and = new And(popLastArgumentMatchers(2));48 matcherStack.push(new LocalizedMatcher(and));49 return new HandyReturnValues();50 }51 /* (non-Javadoc)52 * @see org.mockito.internal.progress.ArgumentMatcherStorage#reportNot()53 */54 public HandyReturnValues reportNot() {55 assertState(!matcherStack.isEmpty(), "No matchers found for Not(?).");56 Not not = new Not(popLastArgumentMatchers(1).get(0));57 matcherStack.push(new LocalizedMatcher(not));58 return new HandyReturnValues();59 }60 private List<Matcher> popLastArgumentMatchers(int count) {61 assertState(!matcherStack.isEmpty(), "No matchers found.");62 assertState(matcherStack.size() >= count,63 "" + count + " matchers expected, " + matcherStack.size() + " recorded.");64 List<Matcher> result = new LinkedList<Matcher>();65 result.addAll(matcherStack.subList(matcherStack.size() - count, matcherStack.size()));66 for (int i = 0; i < count; i++) {67 matcherStack.pop();68 }69 return result;70 }71 private void assertState(boolean toAssert, String message) {72 if (!toAssert) {73 matcherStack.clear();74 throw new InvalidUseOfMatchersException(message);75 }76 }77 /* (non-Javadoc)78 * @see org.mockito.internal.progress.ArgumentMatcherStorage#reportOr()79 */80 public HandyReturnValues reportOr() {81 assertState(!matcherStack.isEmpty(), "No matchers found.");82 Or or = new Or(popLastArgumentMatchers(2));83 matcherStack.push(new LocalizedMatcher(or));84 return new HandyReturnValues();85 }86 /* (non-Javadoc)87 * @see org.mockito.internal.progress.ArgumentMatcherStorage#validateState()88 */89 public void validateState() {90 if (!matcherStack.isEmpty()) {91 LocalizedMatcher lastMatcher = matcherStack.lastElement();92 matcherStack.clear();93 new Reporter().misplacedArgumentMatcher(lastMatcher.getLocation());94 }95 }96 /* (non-Javadoc)97 * @see org.mockito.internal.progress.ArgumentMatcherStorage#reset()98 */99 public void reset() {100 matcherStack.clear();101 }102}...
Source:PowerMockMatchersBinder.java
2import org.hamcrest.Matcher;3import org.mockito.exceptions.Reporter;4import org.mockito.internal.invocation.InvocationMatcher;5import org.mockito.internal.invocation.MatchersBinder;6import org.mockito.internal.matchers.LocalizedMatcher;7import org.mockito.internal.progress.ArgumentMatcherStorage;8import org.mockito.invocation.Invocation;9import java.util.List;10/**11 * This class is essentially a copy of {@link org.mockito.internal.invocation.MatchersBinder} with the exception that12 * the InvocationMatcher is replaced and its toString method is overwritten to avoid exceptions. For why these exceptions happen13 * refer to ToStringGenerator in this package.14 */15public class PowerMockMatchersBinder extends MatchersBinder {16 public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, final Invocation invocation) {17 List<LocalizedMatcher> lastMatchers = argumentMatcherStorage.pullLocalizedMatchers();18 validateMatchers(invocation, lastMatchers);19 final InvocationMatcher invocationWithMatchers = new InvocationMatcher(invocation, (List<Matcher>)(List) lastMatchers) {20 @Override21 public String toString() {22 return invocation.toString();23 }24 };25 return invocationWithMatchers;26 }27 private void validateMatchers(Invocation invocation, List<LocalizedMatcher> lastMatchers) {28 if (!lastMatchers.isEmpty()) {29 int recordedMatchersSize = lastMatchers.size();30 int expectedMatchersSize = invocation.getArguments().length;31 if (expectedMatchersSize != recordedMatchersSize) {32 new Reporter().invalidUseOfMatchers(expectedMatchersSize, lastMatchers);33 }34 }35 }36}...
LocalizedMatcher
Using AI Code Generation
1package org.mockito.internal.matchers;2import org.mockito.ArgumentMatcher;3public class LocalizedMatcher<T> implements ArgumentMatcher<T> {4 private final ArgumentMatcher<T> delegate;5 public LocalizedMatcher(ArgumentMatcher<T> delegate) {6 this.delegate = delegate;7 }8 public boolean matches(Object actual) {9 return delegate.matches(actual);10 }11 public String toString() {12 return delegate.toString();13 }14}15package org.mockito.internal.matchers;16import org.mockito.ArgumentMatcher;17public class LocalizedMatcher<T> implements ArgumentMatcher<T> {18 private final ArgumentMatcher<T> delegate;19 public LocalizedMatcher(ArgumentMatcher<T> delegate) {20 this.delegate = delegate;21 }22 public boolean matches(Object actual) {23 return delegate.matches(actual);24 }25 public String toString() {26 return delegate.toString();27 }28}29package org.mockito.internal.matchers;30import org.mockito.ArgumentMatcher;31public class LocalizedMatcher<T> implements ArgumentMatcher<T> {32 private final ArgumentMatcher<T> delegate;33 public LocalizedMatcher(ArgumentMatcher<T> delegate) {34 this.delegate = delegate;35 }36 public boolean matches(Object actual) {37 return delegate.matches(actual);38 }39 public String toString() {40 return delegate.toString();41 }42}43package org.mockito.internal.matchers;44import org.mockito.ArgumentMatcher;45public class LocalizedMatcher<T> implements ArgumentMatcher<T> {46 private final ArgumentMatcher<T> delegate;47 public LocalizedMatcher(ArgumentMatcher<T> delegate) {48 this.delegate = delegate;49 }50 public boolean matches(Object actual) {51 return delegate.matches(actual);52 }53 public String toString() {54 return delegate.toString();55 }56}57package org.mockito.internal.matchers;58import org.mockito.ArgumentMatcher;59public class LocalizedMatcher<T> implements ArgumentMatcher<T> {60 private final ArgumentMatcher<T> delegate;61 public LocalizedMatcher(ArgumentMatcher<T> delegate) {
LocalizedMatcher
Using AI Code Generation
1package org.mockito.internal.matchers;2import org.mockito.ArgumentMatcher;3public class LocalizedMatcher<T> implements ArgumentMatcher<T> {4 private final ArgumentMatcher<T> matcher;5 private final String description;6 public LocalizedMatcher(ArgumentMatcher<T> matcher, String description) {7 this.matcher = matcher;8 this.description = description;9 }10 public boolean matches(T argument) {11 return matcher.matches(argument);12 }13 public String toString() {14 return description;15 }16}17package org.mockito.internal.matchers;18import org.mockito.ArgumentMatcher;19public class LocalizedMatcher<T> implements ArgumentMatcher<T> {20 private final ArgumentMatcher<T> matcher;21 private final String description;22 public LocalizedMatcher(ArgumentMatcher<T> matcher, String description) {23 this.matcher = matcher;24 this.description = description;25 }26 public boolean matches(T argument) {27 return matcher.matches(argument);28 }29 public String toString() {30 return description;31 }32}33package org.mockito.internal.matchers;34import org.mockito.ArgumentMatcher;35public class LocalizedMatcher<T> implements ArgumentMatcher<T> {36 private final ArgumentMatcher<T> matcher;37 private final String description;38 public LocalizedMatcher(ArgumentMatcher<T> matcher, String description) {39 this.matcher = matcher;40 this.description = description;41 }42 public boolean matches(T argument) {43 return matcher.matches(argument);44 }45 public String toString() {46 return description;47 }48}49package org.mockito.internal.matchers;50import org.mockito.ArgumentMatcher;51public class LocalizedMatcher<T> implements ArgumentMatcher<T> {52 private final ArgumentMatcher<T> matcher;53 private final String description;54 public LocalizedMatcher(ArgumentMatcher<T> matcher, String description) {55 this.matcher = matcher;56 this.description = description;57 }58 public boolean matches(T argument) {59 return matcher.matches(argument);60 }61 public String toString() {62 return description;63 }64}65package org.mockito.internal.matchers;66import org.mockito.ArgumentMatcher;67public class LocalizedMatcher<T> implements ArgumentMatcher<T> {68 private final ArgumentMatcher<T> matcher;
LocalizedMatcher
Using AI Code Generation
1public class Test {2 public void test() {3 LocalizedMatcher localizedMatcher = new LocalizedMatcher(null);4 localizedMatcher.toString();5 }6}7public class Test {8 public void test() {9 LocalizedMatcher localizedMatcher = new LocalizedMatcher(null);10 localizedMatcher.toString();11 }12}
LocalizedMatcher
Using AI Code Generation
1package org.mockito.internal.matchers;2import java.util.Locale;3import org.junit.Test;4import org.junit.runner.RunWith;5import org.mockito.Mock;6import org.mockito.junit.MockitoJUnitRunner;7import static org.mockito.Mockito.*;8@RunWith(MockitoJUnitRunner.class)9public class LocalizedMatcherTest {10 private Locale locale;11 private Object object;12 public void testLocalizedMatcher() {13 LocalizedMatcher localizedMatcher = new LocalizedMatcher();14 localizedMatcher.matches(object);15 verify(object).toString();16 verify(object).toString(locale);17 }18}19package org.mockito.internal.matchers;20import java.util.Locale;21import org.junit.Test;22import org.junit.runner.RunWith;23import org.mockito.Mock;24import org.mockito.junit.MockitoJUnitRunner;25import static org.mockito.Mockito.*;26@RunWith(MockitoJUnitRunner.class)27public class LocalizedMatcherTest {28 private Locale locale;29 private Object object;30 public void testLocalizedMatcher() {31 LocalizedMatcher localizedMatcher = new LocalizedMatcher();32 localizedMatcher.matches(object);33 verify(object).toString();34 verify(object).toString(locale);35 }36}37package org.mockito.internal.matchers;38import java.util.Locale;39import org.junit.Test;40import org.junit.runner.RunWith;41import org.mockito.Mock;42import org.mockito.junit.MockitoJUnitRunner;43import static org.mockito.Mockito.*;44@RunWith(MockitoJUnitRunner.class)45public class LocalizedMatcherTest {46 private Locale locale;47 private Object object;48 public void testLocalizedMatcher() {49 LocalizedMatcher localizedMatcher = new LocalizedMatcher();50 localizedMatcher.matches(object);51 verify(object).toString();52 verify(object).toString(locale);53 }54}55package org.mockito.internal.matchers;56import java.util.Locale;57import org.junit.Test;58import org.junit.runner.RunWith;59import org.mockito.Mock;60import org.mockito.junit.MockitoJUnitRunner;61import static org.mockito.Mockito.*;62@RunWith(MockitoJUnitRunner.class)63public class LocalizedMatcherTest {64 private Locale locale;
LocalizedMatcher
Using AI Code Generation
1import org.mockito.internal.matchers.LocalizedMatcher;2import org.mockito.internal.matchers.Equals;3public class MockitoExample {4 public static void main(String[] args) {5 LocalizedMatcher localizedMatcher1 = new LocalizedMatcher(new Equals("mockito"));6 LocalizedMatcher localizedMatcher2 = new LocalizedMatcher(new Equals("mo
LocalizedMatcher
Using AI Code Generation
1import org.mockito.ArgumentMatcher;2import org.mockito.internal.matchers.LocalizedMatcher;3public class MockitoTest {4 public static void main(String[] args) {5 LocalizedMatcher localizedMatcher = new LocalizedMatcher();6 ArgumentMatcher argumentMatcher = new ArgumentMatcher() {7 public boolean matches(Object o) {8 return localizedMatcher.matches(o);9 }10 };11 System.out.println(argumentMatcher.matches("test"));12 }13}14org.mockito.internal.matchers.LocalizedMatcher.matches(Object o) method15The matches(Object o) method of org.mockito.internal.matchers.LocalizedMatcher class is defined as follows:16public boolean matches(Object o) {17 return o != null && o.equals(getExpected());18}19public interface ArgumentMatcher<T> {20 boolean matches(T argument);21}
LocalizedMatcher
Using AI Code Generation
1import org.mockito.internal.matchers.LocalizedMatcher;2public class 1 {3 public static void main(String[] args) {4 LocalizedMatcher m = new LocalizedMatcher("mockito");5 System.out.println(m.matches("mockito"));6 System.out.println(m.toString());7 }8}9Argument(s) are different! Wanted:10-> at 1.main(1.java:11)
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.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!