Skip to content

Instantly share code, notes, and snippets.

@jacek-marchwicki
Created March 25, 2015 14:25
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jacek-marchwicki/006d4c5f61158cfef39a to your computer and use it in GitHub Desktop.
Save jacek-marchwicki/006d4c5f61158cfef39a to your computer and use it in GitHub Desktop.
LastCall mockito verification
/*
* Copyright 2015 Jacek Marchwicki <jacek.marchwicki@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.appunite.rx.test;
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 javax.annotation.Nonnull;
import static org.mockito.internal.util.StringJoiner.join;
public class LastCall implements VerificationMode {
public static VerificationMode lastCall() {
return new LastCall();
}
public LastCall() {
}
public void verify(@Nonnull VerificationData data) {
final List<Invocation> invocations = data.getAllInvocations();
final InvocationMatcher wanted = data.getWanted();
final Invocation lastInvocation = invocations.get(invocations.size() - 1);
if (!wanted.matches(lastInvocation)) {
argumentsAreDifferent(wanted, lastInvocation);
}
}
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());
}
public 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);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment