Skip to content

Commit

Permalink
Pushed complexity to a separate object
Browse files Browse the repository at this point in the history
Issue #384
  • Loading branch information
mockitoguy committed Jul 31, 2016
1 parent d875684 commit 8987022
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 15 deletions.
@@ -0,0 +1,42 @@
package org.mockito.internal.runners;

import org.mockito.internal.util.MockitoLogger;
import org.mockito.invocation.Invocation;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

/**
* Renders the stubbing hint on argument mismatches
*/
class StubbingArgMismatches {

Map<Invocation, Set<Invocation>> mismatches = new HashMap<Invocation, Set<Invocation>>();

public void add(Invocation invocation, Invocation stubbing) {
Set<Invocation> matchingInvocations = mismatches.get(stubbing);
if (matchingInvocations == null) {
matchingInvocations = new HashSet<Invocation>();
mismatches.put(stubbing, matchingInvocations);
}
matchingInvocations.add(invocation);
}

public void log(MockitoLogger logger) {
if (mismatches.isEmpty()) {
return;
}

StringBuilder out = new StringBuilder("[MockitoHint] See javadoc for MockitoHint class.\n");
int x = 1;
for (Map.Entry<Invocation, Set<Invocation>> m : mismatches.entrySet()) {
out.append("[MockitoHint] ").append(x++).append(". unused stub ")
.append(m.getKey().getLocation()).append("\n");
out.append("[MockitoHint] similar call ").append(m.getValue().iterator().next().getLocation());
}

logger.log(out.toString());
}
}
Expand Up @@ -61,29 +61,17 @@ public void validateUnusedStubs(Class<?> testClass, RunNotifier notifier) {
//TODO 384 I'm not completely happy with putting this functionality in this listener.
//TODO 384 Perhaps add an interfaces for the clients
public void printStubbingMismatches(MockitoLogger logger) {
Map<Invocation, Invocation> argMismatches = new LinkedHashMap<Invocation, Invocation>();
StubbingArgMismatches mismatches = new StubbingArgMismatches();
for (Invocation i : unstubbedInvocations) {
for (Invocation stubbing : stubbings.values()) {
//method name & mock matches
if (stubbing.getMock() == i.getMock()
&& stubbing.getMethod().getName().equals(i.getMethod().getName())) {
argMismatches.put(i, stubbing);
mismatches.add(i, stubbing);
}
}
}
if (argMismatches.isEmpty()) {
return;
}

StringBuilder out = new StringBuilder("[MockitoHint] See javadoc for MockitoHint class.\n");
int x = 1;
for (Invocation i : argMismatches.keySet()) {
out.append("[MockitoHint] ").append(x++).append(". unused stub ")
.append(argMismatches.get(i).getLocation()).append("\n");
out.append("[MockitoHint] similar call ").append(i.getLocation());
}

logger.log(out.toString());
mismatches.log(logger);
}

public String printUnusedStubbings(MockitoLogger logger) {
Expand Down
Expand Up @@ -88,6 +88,7 @@ public void evaluate() throws Throwable {
// arg_mismatches_reports_unused_stubbings_only_when_multiple_similar_stubbings?
// arg_mismatches_reports_unused_stubbings_first_when_multiple_similar_stubbings?
// arg_mismatches_reports_stubbings_orderly_when_multiple_similar_stubbings
// arg_mismatch_with_multiple_stubbings_and_multiple_invocations
// arg_mismatches_when_similar_stubbings_declared_in_same_line
// arg_mismatches_when_invocations_triggered_in_same_line
// arg_mismatches_when_similar_stubbings_was_used_before
Expand Down

0 comments on commit 8987022

Please sign in to comment.