Last active
October 12, 2018 07:14
-
-
Save ZumiKua/fba69544c567d0ab1e537342625289af to your computer and use it in GitHub Desktop.
Custom VerificationMode for ignoring orders of two invocations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.mockito.exceptions.base.MockitoException; | |
import org.mockito.internal.verification.VerificationModeFactory; | |
import org.mockito.internal.verification.api.VerificationData; | |
import org.mockito.internal.verification.api.VerificationDataInOrder; | |
import org.mockito.internal.verification.api.VerificationInOrderMode; | |
import org.mockito.invocation.Invocation; | |
import org.mockito.invocation.MatchableInvocation; | |
import org.mockito.verification.VerificationMode; | |
import java.util.ArrayList; | |
import java.util.HashSet; | |
import java.util.List; | |
import static org.mockito.internal.verification.checkers.MissingInvocationChecker.checkMissingInvocation; | |
import static org.mockito.internal.verification.checkers.NumberOfInvocationsChecker.checkNumberOfInvocationsNonGreedy; | |
public class IgnoreOrder{ | |
HashSet<MatchableInvocation> mWants = new HashSet<>(); | |
public Ignore ignoreOrder(int count){ | |
return new Ignore(count); | |
} | |
class Ignore implements VerificationInOrderMode, VerificationMode{ | |
private final int mCount; | |
Ignore(int count){ | |
mCount = count; | |
} | |
@Override | |
public void verifyInOrder(VerificationDataInOrder data) { | |
List<Invocation> invocations = data.getAllInvocations(); | |
checkMissingInvocation(invocations, data.getWanted()); | |
List<Invocation> newInvocations = new ArrayList<>(); | |
for (Invocation invocation : invocations) { | |
boolean isVerifiedAndShouldIgnore = false; | |
if(invocation.isVerified()){ | |
for (MatchableInvocation want : mWants) { | |
if(want.matches(invocation)){ | |
isVerifiedAndShouldIgnore = true; | |
break; | |
} | |
} | |
} | |
if(!isVerifiedAndShouldIgnore){ | |
newInvocations.add(invocation); | |
} | |
} | |
checkNumberOfInvocationsNonGreedy(newInvocations, data.getWanted(), mCount, data.getOrderingContext()); | |
mWants.add(data.getWanted()); | |
} | |
@Override | |
public void verify(VerificationData data) { | |
throw new MockitoException("ignoreOrder is only intended to work with InOrder"); | |
} | |
@Override | |
public VerificationMode description(String description) { | |
return VerificationModeFactory.description(this, description); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Test | |
public void testIgnoreOrder(){ | |
List a = mock(List.class); | |
List b = mock(List.class); | |
a.iterator(); | |
a.size(); | |
b.size(); | |
a.size(); | |
b.size(); | |
a.size(); | |
InOrder order = inOrder(a,b); | |
IgnoreOrder io = new IgnoreOrder(); | |
order.verify(a).iterator(); | |
order.verify(a, io.ignoreOrder(2)).size(); | |
order.verify(b, io.ignoreOrder(2)).size(); | |
order.verify(a, times(1)).size(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment