Best Mockito code snippet using org.mockito.internal.exceptions.Reporter.reportNoSubMatchersFound
Source:ArgumentMatcherStorageImpl.java
...10import org.mockito.internal.matchers.Or;11import static java.util.Collections.emptyList;12import static org.mockito.internal.exceptions.Reporter.incorrectUseOfAdditionalMatchers;13import static org.mockito.internal.exceptions.Reporter.misplacedArgumentMatcher;14import static org.mockito.internal.exceptions.Reporter.reportNoSubMatchersFound;15import java.util.*;16public class ArgumentMatcherStorageImpl implements ArgumentMatcherStorage {17 private static final int TWO_SUB_MATCHERS = 2;18 private static final int ONE_SUB_MATCHER = 1;19 private final Stack<LocalizedMatcher> matcherStack = new Stack<LocalizedMatcher>();20 public void reportMatcher(ArgumentMatcher<?> matcher) {21 matcherStack.push(new LocalizedMatcher(matcher));22 }23 public List<LocalizedMatcher> pullLocalizedMatchers() {24 if (matcherStack.isEmpty()) {25 return emptyList();26 }27 List<LocalizedMatcher> lastMatchers = resetStack();28 return lastMatchers;29 }30 public void reportAnd() {31 assertStateFor("And(?)", TWO_SUB_MATCHERS);32 ArgumentMatcher<?> m1 = popMatcher();33 ArgumentMatcher<?> m2 = popMatcher();34 reportMatcher(new And(m1, m2));35 }36 public void reportOr() {37 assertStateFor("Or(?)", TWO_SUB_MATCHERS);38 ArgumentMatcher<?> m1 = popMatcher();39 ArgumentMatcher<?> m2 = popMatcher();40 reportMatcher(new Or(m1, m2));41 }42 public void reportNot() {43 assertStateFor("Not(?)", ONE_SUB_MATCHER);44 ArgumentMatcher<?> m = popMatcher();45 reportMatcher(new Not(m));46 }47 public void validateState() {48 if (!matcherStack.isEmpty()) {49 List<LocalizedMatcher> lastMatchers = resetStack();50 throw misplacedArgumentMatcher(lastMatchers);51 }52 }53 public void reset() {54 matcherStack.clear();55 }56 private void assertStateFor(String additionalMatcherName, int subMatchersCount) {57 if (matcherStack.isEmpty()) {58 throw reportNoSubMatchersFound(additionalMatcherName);59 }60 if (matcherStack.size() < subMatchersCount) {61 List<LocalizedMatcher> lastMatchers = resetStack();62 throw incorrectUseOfAdditionalMatchers(additionalMatcherName, subMatchersCount, lastMatchers);63 }64 }65 private ArgumentMatcher<?> popMatcher() {66 return matcherStack.pop().getMatcher();67 }68 private List<LocalizedMatcher> resetStack() {69 ArrayList<LocalizedMatcher> lastMatchers = new ArrayList<LocalizedMatcher>(matcherStack);70 reset();71 return lastMatchers;72 }...
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!!