Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"But invoked here" lists the invocation parameters #2259

Merged
merged 2 commits into from Apr 7, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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