Skip to content

Commit

Permalink
"But invoked here" lists the invocation parameters (#2259)
Browse files Browse the repository at this point in the history
Fixes #2058

Co-authored-by: EugeneLesnov <zheka.lesnov1996@gmail.com>
  • Loading branch information
TimvdLippe and eugenelesnov committed Apr 7, 2021
1 parent f9e4bca commit d3d11f4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 11 deletions.
24 changes: 19 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,15 @@ private static String createTooManyInvocationsMessage(
}

public static MockitoAssertionError neverWantedButInvoked(
DescribedInvocation wanted, List<Location> invocations) {
DescribedInvocation wanted, List<Invocation> invocations) {

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

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

private static String createAllLocationsArgsMessage(List<Invocation> invocations) {
StringBuilder sb = new StringBuilder();
for (Invocation invocation : invocations) {
Location location = invocation.getLocation();
if (location == null) {
continue;
}
sb.append(location)
.append(" with arguments: ")
.append(Arrays.toString(invocation.getArguments()))
.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,33 @@ 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() + " with arguments: [arg1]");

NumberOfInvocationsChecker.checkNumberOfInvocations(invocations, wanted, 0);
}

@Test
public void shouldReportNeverWantedButInvokedWithArgs_multipleInvocations() throws Exception {
Invocation first = buildSimpleMethodWithArgs("arg1").toInvocation();
Invocation second = buildSimpleMethodWithArgs("arg1").toInvocation();

invocations = asList(first, second);
wanted = buildSimpleMethodWithArgs("arg1").toInvocationMatcher();

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

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

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

private InvocationBuilder buildDifferentMethodWithArgs(String arg) {
return new InvocationBuilder().mock(mock).differentMethod().args(arg);
}

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

0 comments on commit d3d11f4

Please sign in to comment.