Skip to content

Commit

Permalink
Shuffled TODOs, added perf tweaks and comments
Browse files Browse the repository at this point in the history
Issue #384
  • Loading branch information
mockitoguy committed Jul 31, 2016
1 parent fa1a796 commit 35807a3
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
Expand Up @@ -44,7 +44,7 @@ public void run(RunNotifier notifier) {
//1. if all tests from given test have ran (filter requested is false)
// Otherwise we would report unnecessary stubs even if the user runs just single test from the class
//2. tests are successful (we don't want to add an extra failure on top of any existing failure, to avoid confusion)
//TODO JUnit runner should have a specific message explaining to the user how it works.
//TODO 384 JUnit runner should have a specific message explaining to the user how it works.
reporter.validateUnusedStubs(testClass, notifier);
}
}
Expand Down
Expand Up @@ -10,11 +10,10 @@
import java.util.*;

/**
* Created by sfaber on 5/6/16.
* Reports unnecessary stubbings
*/
//TODO 384 package rework, merge internal.junit with internal.runners.
//TODO 384 make it thread safe so that users don't have to worry
//TODO 384 create MockitoHint class
//TODO 384 what if the user uses both: runner and the rule?
class UnnecessaryStubbingsReporter implements StubbingListener {

private final Map<String, Invocation> stubbings = new LinkedHashMap<String, Invocation>();
Expand All @@ -34,13 +33,30 @@ public void newStubbing(Invocation stubbing) {
public void usedStubbing(Invocation stubbing, Invocation actual) {
String location = stubbing.getLocation().toString();
used.add(location);

//perf tweak, let's get rid of used stubbing now, less calculation later
stubbings.remove(location);
}

public void stubbingNotFound(Invocation actual) {}

void validateUnusedStubs(Class<?> testClass, RunNotifier notifier) {
if (stubbings.isEmpty()) {
//perf tweak, bailing out early to avoid extra computation
return;
}

//removing all used stubbings accounting for possible constructor / @Before stubbings
// that were used only in specific test methods (e.g. not all test methods)
for (String u : used) {
stubbings.remove(u); //TODO 384 state manipulation
//it's awkward to manipulate the state of this object here,
// we cannot safely rerun validateUnusedStubs() method.
// However, we get good performance and simpler code.
stubbings.remove(u);
}

if (stubbings.isEmpty()) {
return;
}

if (stubbings.isEmpty()) {
Expand Down

0 comments on commit 35807a3

Please sign in to comment.