Skip to content

Commit

Permalink
"But invoked here" lists the invocation parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
eugenelesnov committed Jan 22, 2021
1 parent 0630886 commit 5eb12b8
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
28 changes: 23 additions & 5 deletions src/main/java/org/mockito/internal/exceptions/Reporter.java
Expand Up @@ -10,9 +10,7 @@

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.*;

import org.mockito.exceptions.base.MockitoAssertionError;
import org.mockito.exceptions.base.MockitoException;
Expand Down Expand Up @@ -408,14 +406,17 @@ private static String createTooManyInvocationsMessage(
}

public static MockitoAssertionError neverWantedButInvoked(
DescribedInvocation wanted, List<Location> invocations) {
DescribedInvocation wanted, List<Invocation> invocations) {
Map<Location, Object[]> locationArgs = new HashMap<>();
invocations.forEach(inv -> locationArgs.put(inv.getLocation(), inv.getArguments()));

return new NeverWantedButInvoked(
join(
wanted.toString(),
"Never wanted here:",
new LocationImpl(),
"But invoked here:",
createAllLocationsMessage(invocations)));
createAllLocationsArgsMessage(locationArgs)));
}

public static MockitoAssertionError tooManyActualInvocationsInOrder(
Expand All @@ -439,6 +440,23 @@ private static String createAllLocationsMessage(List<Location> locations) {
return sb.toString();
}

private static String createAllLocationsArgsMessage(Map<Location, Object[]> locationArgs) {
if (locationArgs == null || locationArgs.isEmpty()) {
return "";
}
StringBuilder sb = new StringBuilder();
locationArgs.forEach((location, args) -> {
if (location == null) {
return;
}
sb.append(location)
.append(" with next arguments: ")
.append(Arrays.toString(args))
.append("\n");
});
return sb.toString();
}

private static String createTooFewInvocationsMessage(
org.mockito.internal.reporting.Discrepancy discrepancy,
DescribedInvocation wanted,
Expand Down
Expand Up @@ -40,7 +40,7 @@ public static void checkNumberOfInvocations(
new Discrepancy(wantedCount, actualCount), wanted, allLocations);
}
if (wantedCount == 0 && actualCount > 0) {
throw neverWantedButInvoked(wanted, getAllLocations(actualInvocations));
throw neverWantedButInvoked(wanted, actualInvocations);
}
if (wantedCount < actualCount) {
throw tooManyActualInvocations(
Expand Down
Expand Up @@ -9,6 +9,7 @@

import static org.assertj.core.api.Assertions.assertThat;

import java.util.Collections;
import java.util.List;

import org.hamcrest.BaseMatcher;
Expand Down Expand Up @@ -116,16 +117,17 @@ public void shouldReportTooManyActual() throws Exception {
}

@Test
public void shouldReportNeverWantedButInvoked() throws Exception {
Invocation first = buildSimpleMethod().toInvocation();
public void shouldReportNeverWantedButInvokedWithArgs() throws Exception {
Invocation invocation = buildSimpleMethodWithArgs("arg1").toInvocation();

invocations = asList(first);
wanted = buildSimpleMethod().toInvocationMatcher();
invocations = Collections.singletonList(invocation);
wanted = buildSimpleMethodWithArgs("arg1").toInvocationMatcher();

exception.expect(NeverWantedButInvoked.class);
exception.expectMessage("Never wanted here");
exception.expectMessage("But invoked here");
exception.expectMessage("" + first.getLocation());
exception.expectMessage("" + invocation.getLocation());
exception.expectMessage("with next arguments: [arg1]");

NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 0);
}
Expand All @@ -145,6 +147,10 @@ private InvocationBuilder buildSimpleMethod() {
return new InvocationBuilder().mock(mock).simpleMethod();
}

private InvocationBuilder buildSimpleMethodWithArgs(String arg) {
return new InvocationBuilder().mock(mock).simpleMethod().args(arg);
}

private static BaseMatcher<String> containsTimes(String value, int amount) {
return new StringContainsNumberMatcher(value, amount);
}
Expand Down

0 comments on commit 5eb12b8

Please sign in to comment.