-
-
Save sockeqwe/ea9cf969ffafc99b0657c47a0199f700 to your computer and use it in GitHub Desktop.
Mockito verification of the last call on a method
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
package com.pascalwelsch.mockito; | |
import org.mockito.exceptions.base.MockitoException; | |
import org.mockito.exceptions.verification.ArgumentsAreDifferent; | |
import org.mockito.internal.debugging.LocationImpl; | |
import org.mockito.internal.invocation.InvocationMatcher; | |
import org.mockito.internal.junit.JUnitTool; | |
import org.mockito.internal.reporting.SmartPrinter; | |
import org.mockito.internal.verification.api.VerificationData; | |
import org.mockito.internal.verification.argumentmatching.ArgumentMatchingTool; | |
import org.mockito.invocation.Invocation; | |
import org.mockito.invocation.Location; | |
import org.mockito.verification.VerificationMode; | |
import java.util.List; | |
import static org.mockito.internal.util.StringJoiner.join; | |
public class LastCall implements VerificationMode { | |
public static LastCall lastCall() { | |
return new LastCall(); | |
} | |
public void verify(VerificationData data) { | |
List<Invocation> invocations = data.getAllInvocations(); | |
InvocationMatcher matcher = data.getWanted(); | |
for (int i = invocations.size() - 1; i >= 0; i--) { | |
final Invocation invocation = invocations.get(i); | |
if (invocation.getMethod().equals(matcher.getMethod())) { | |
if (!matcher.matches(invocation)) { | |
// throw | |
argumentsAreDifferent(matcher, invocation); | |
} else { | |
// match | |
return; | |
} | |
} | |
} | |
throw new MockitoException("Not invoked at all"); | |
} | |
private void argumentsAreDifferent(String wanted, String actual, Location actualLocation) { | |
final String message = join("Argument(s) for last call are different! Wanted:", | |
wanted, | |
new LocationImpl(), | |
"Actual invocation has different arguments:", | |
actual, | |
actualLocation, | |
"" | |
); | |
if (JUnitTool.hasJUnit()) { | |
throw JUnitTool.createArgumentsAreDifferentException(message, wanted, actual); | |
} else { | |
throw new ArgumentsAreDifferent(message); | |
} | |
} | |
private void argumentsAreDifferent(InvocationMatcher wanted, Invocation invocation) { | |
final Integer[] indicesOfSimilarMatchingArguments = | |
new ArgumentMatchingTool() | |
.getSuspiciouslyNotMatchingArgsIndexes(wanted.getMatchers(), | |
invocation.getArguments()); | |
final SmartPrinter smartPrinter = new SmartPrinter(wanted, invocation, | |
indicesOfSimilarMatchingArguments); | |
argumentsAreDifferent(smartPrinter.getWanted(), smartPrinter.getActual(), | |
invocation.getLocation()); | |
} | |
} |
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 static com.pascalwelsch.mockito.LastCall.lastCall; | |
// usage | |
verify(view, lastCall()).showLoadingIndicator(false); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment