Created
July 5, 2019 10:02
-
-
Save paulkagiri/565d8a46533e0e8227fb068a80971cbc to your computer and use it in GitHub Desktop.
Mockito Last Invocation Helper
This file contains 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.api.VerificationData; | |
import org.mockito.invocation.Invocation; | |
import org.mockito.invocation.MatchableInvocation; | |
import org.mockito.verification.VerificationMode; | |
import java.util.List; | |
public class LastInvocationHelper { | |
private static class LastInvocation implements VerificationMode { | |
@Override | |
public void verify(VerificationData data) { | |
List<Invocation> invocations = data.getAllInvocations(); | |
MatchableInvocation matcher = data.getTarget(); | |
Invocation invocation = invocations.get(invocations.size() - 1); | |
if (!matcher.matches(invocation)) { | |
throw new MockitoException("Last call did not match!" + | |
"\nWanted: " + matcher.toString() + "\nBut got: " + invocation.toString()); | |
} | |
} | |
@Override | |
public VerificationMode description(String description) { | |
return null; | |
} | |
} | |
public static LastInvocation lastInvocation() { | |
return new LastInvocation(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment