How to use varargLength method of org.mockito.internal.invocation.MatcherApplicationStrategy class

Best Mockito code snippet using org.mockito.internal.invocation.MatcherApplicationStrategy.varargLength

Source:MatcherApplicationStrategy.java Github

copy

Full Screen

...18 private final MatcherApplicationType matchingType;19 private MatcherApplicationStrategy(Invocation invocation, List<ArgumentMatcher<?>> matchers, MatcherApplicationType matchingType) {20 this.invocation = invocation;21 if (matchingType == MATCH_EACH_VARARGS_WITH_LAST_MATCHER) {22 int times = varargLength(invocation);23 this.matchers = appendLastMatcherNTimes(matchers, times);24 } else {25 this.matchers = matchers;26 }27 this.matchingType = matchingType;28 }29 /**30 * Returns the {@link MatcherApplicationStrategy} that must be used to capture the31 * arguments of the given <b>invocation</b> using the given <b>matchers</b>.32 *33 * @param invocation34 * that contain the arguments to capture35 * @param matchers36 * that will be used to capture the arguments of the invocation,37 * the passed {@link List} is not required to contain a38 * {@link CapturingMatcher}39 * @return never <code>null</code>40 */41 public static MatcherApplicationStrategy getMatcherApplicationStrategyFor(Invocation invocation, List<ArgumentMatcher<?>> matchers) {42 MatcherApplicationType type = getMatcherApplicationType(invocation, matchers);43 return new MatcherApplicationStrategy(invocation, matchers, type);44 }45 /**46 * Applies the given {@link ArgumentMatcherAction} to all arguments and47 * corresponding matchers48 *49 * @param action50 * must not be <code>null</code>51 * @return52 * <ul>53 * <li><code>true</code> if the given <b>action</b> returned54 * <code>true</code> for all arguments and matchers passed to it.55 * <li><code>false</code> if the given <b>action</b> returned56 * <code>false</code> for one of the passed arguments and matchers57 * <li><code>false</code> if the given matchers don't fit to the given invocation58 * because too many or to few matchers are available.59 * </ul>60 */61 public boolean forEachMatcherAndArgument(ArgumentMatcherAction action) {62 if (matchingType == ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS)63 return false;64 Object[] arguments = invocation.getArguments();65 for (int i = 0; i < arguments.length; i++) {66 ArgumentMatcher<?> matcher = matchers.get(i);67 Object argument = arguments[i];68 if (!action.apply(matcher, argument)) {69 return false;70 }71 }72 return true;73 }74 private static MatcherApplicationType getMatcherApplicationType(Invocation invocation, List<ArgumentMatcher<?>> matchers) {75 final int rawArguments = invocation.getRawArguments().length;76 final int expandedArguments = invocation.getArguments().length;77 final int matcherCount = matchers.size();78 if (expandedArguments == matcherCount) {79 return ONE_MATCHER_PER_ARGUMENT;80 }81 if (rawArguments == matcherCount && isLastMatcherVargargMatcher(matchers)) {82 return MATCH_EACH_VARARGS_WITH_LAST_MATCHER;83 }84 return ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS;85 }86 private static boolean isLastMatcherVargargMatcher(final List<ArgumentMatcher<?>> matchers) {87 return lastMatcher(matchers) instanceof VarargMatcher;88 }89 private static List<ArgumentMatcher<?>> appendLastMatcherNTimes(List<ArgumentMatcher<?>> matchers, int timesToAppendLastMatcher) {90 ArgumentMatcher<?> lastMatcher = lastMatcher(matchers);91 List<ArgumentMatcher<?>> expandedMatchers = new ArrayList<ArgumentMatcher<?>>(matchers);92 for (int i = 0; i < timesToAppendLastMatcher; i++) {93 expandedMatchers.add(lastMatcher);94 }95 return expandedMatchers;96 }97 private static int varargLength(Invocation invocation) {98 int rawArgumentCount = invocation.getRawArguments().length;99 int expandedArgumentCount = invocation.getArguments().length;100 return expandedArgumentCount - rawArgumentCount;101 }102 private static ArgumentMatcher<?> lastMatcher(List<ArgumentMatcher<?>> matchers) {103 return matchers.get(matchers.size() - 1);104 }105 enum MatcherApplicationType {106 ONE_MATCHER_PER_ARGUMENT, MATCH_EACH_VARARGS_WITH_LAST_MATCHER, ERROR_UNSUPPORTED_NUMBER_OF_MATCHERS;107 }108}...

Full Screen

Full Screen

varargLength

Using AI Code Generation

copy

Full Screen

1public class VarargsLengthTest {2 public void testVarargsLength() {3 MatcherApplicationStrategy matcherApplicationStrategy = new MatcherApplicationStrategy();4 Object[] args = new Object[] { 1, 2, 3 };5 int varargsLength = matcherApplicationStrategy.varargsLength(args);6 assertThat(varargsLength, is(0));7 }8}9at org.mockito.internal.util.MockUtil.createMockSettings(MockUtil.java:72)10at org.mockito.internal.util.MockUtil.createMock(MockUtil.java:52)11at org.mockito.internal.MockitoCore.mock(MockitoCore.java:57)12at org.mockito.Mockito.mock(Mockito.java:1519)13at org.mockito.Mockito.mock(Mockito.java:1424)14at com.baeldung.mockito.VarargsLengthTest.testVarargsLength(VarargsLengthTest.java:10)

Full Screen

Full Screen

varargLength

Using AI Code Generation

copy

Full Screen

1 public void testVarargLength() {2 MatcherApplicationStrategy matcherApplicationStrategy = new MatcherApplicationStrategy();3 Object[] varargs = new Object[3];4 varargs[0] = "a";5 varargs[1] = "b";6 varargs[2] = "c";7 int varargLength = matcherApplicationStrategy.varargLength(varargs);8 assertEquals(3, varargLength);9 }10}

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.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful