Best Mockito code snippet using org.mockito.internal.exceptions.Reporter.wantedAtMostX
Source:AtMost.java
2 * Copyright (c) 2007 Mockito contributors3 * This program is made available under the terms of the MIT License.4 */5package org.mockito.internal.verification;6import static org.mockito.internal.exceptions.Reporter.wantedAtMostX;7import static org.mockito.internal.invocation.InvocationMarker.markVerified;8import static org.mockito.internal.invocation.InvocationsFinder.findInvocations;9import java.util.Iterator;10import java.util.List;11import org.mockito.exceptions.base.MockitoException;12import org.mockito.internal.verification.api.VerificationData;13import org.mockito.invocation.Invocation;14import org.mockito.invocation.MatchableInvocation;15import org.mockito.verification.VerificationMode;16public class AtMost implements VerificationMode {17 private final int maxNumberOfInvocations;18 public AtMost(int maxNumberOfInvocations) {19 if (maxNumberOfInvocations < 0) {20 throw new MockitoException("Negative value is not allowed here");21 }22 this.maxNumberOfInvocations = maxNumberOfInvocations;23 }24 public void verify(VerificationData data) {25 List<Invocation> invocations = data.getAllInvocations();26 MatchableInvocation wanted = data.getTarget();27 List<Invocation> found = findInvocations(invocations, wanted);28 int foundSize = found.size();29 if (foundSize > maxNumberOfInvocations) {30 throw wantedAtMostX(maxNumberOfInvocations, foundSize);31 }32 removeAlreadyVerified(found);33 markVerified(found, wanted);34 }35 private void removeAlreadyVerified(List<Invocation> invocations) {36 for (Iterator<Invocation> iterator = invocations.iterator(); iterator.hasNext(); ) {37 Invocation i = iterator.next();38 if (i.isVerified()) {39 iterator.remove();40 }41 }42 }43}...
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!!