public int compare(Stubbing o1, Stubbing o2) { return invocationComparator.compare(o1.getInvocation(), o2.getInvocation()); } }
void reportUnused() { if (unused.isEmpty()) { return; } List<Invocation> invocations = new LinkedList<Invocation>(); for (Stubbing stubbing : unused) { invocations.add(stubbing.getInvocation()); } if (invocations.isEmpty()) { return; } Reporter.unncessaryStubbingException(invocations); } }
private static List<Invocation> potentialArgMismatches(Invocation invocation, Collection<Stubbing> stubbings) { List<Invocation> matchingStubbings = new LinkedList<Invocation>(); for (Stubbing s : stubbings) { if (UnusedStubbingReporting.shouldBeReported(s) && s.getInvocation().getMethod().getName().equals(invocation.getMethod().getName()) //If stubbing and invocation are in the same source file we assume they are in the test code, // and we don't flag it as mismatch: && !s.getInvocation().getLocation().getSourceFile().equals(invocation.getLocation().getSourceFile())) { matchingStubbings.add(s.getInvocation()); } } return matchingStubbings; }
/** * Finds all unused stubs for given mocks * * @param mocks full list of mocks */ public List<Invocation> find(List<?> mocks) { List<Invocation> unused = new LinkedList<Invocation>(); for (Object mock : mocks) { List<Stubbing> fromSingleMock = MockUtil.getInvocationContainer(mock).getStubbingsDescending(); for(Stubbing s : fromSingleMock) { if (!s.wasUsed()) { unused.add(s.getInvocation()); } } } return unused; } }
sb.append(" ").append(x++).append(". ").append(s.getInvocation()).append("\n"); sb.append(" - stubbed ").append(s.getInvocation().getLocation()).append("\n");
for (Stubbing s : stubbings) { if (!UnusedStubbingReporting.shouldBeReported(s)) { String location = s.getInvocation().getLocation().toString(); locationsOfUsedStubbings.add(location); String location = s.getInvocation().getLocation().toString(); if (!locationsOfUsedStubbings.contains(location)) { out.put(location, s.getInvocation());
StubbingArgMismatches getStubbingArgMismatches(Iterable<?> mocks) { StubbingArgMismatches mismatches = new StubbingArgMismatches(); for (Invocation i : AllInvocationsFinder.find(mocks)) { if (i.stubInfo() != null) { continue; } for (Stubbing stubbing : AllInvocationsFinder.findStubbings(mocks)) { //method name & mock matches if (!stubbing.wasUsed() && stubbing.getInvocation().getMock() == i.getMock() && stubbing.getInvocation().getMethod().getName().equals(i.getMethod().getName())) { mismatches.add(i, stubbing.getInvocation()); } } } return mismatches; } }
void format(String testName, MockitoLogger logger) { if (unused.isEmpty()) { return; } StubbingHint hint = new StubbingHint(testName); int x = 1; for (Stubbing candidate : unused) { if (!candidate.wasUsed()) { hint.appendLine(x++, ". Unused ", candidate.getInvocation().getLocation()); } } logger.log(hint.toString()); }
private Object deepStub(InvocationOnMock invocation, GenericMetadataSupport returnTypeGenericMetadata) throws Throwable { InvocationContainerImpl container = MockUtil.getInvocationContainer(invocation.getMock()); // matches invocation for verification // TODO why don't we do container.findAnswer here? for (Stubbing stubbing : container.getStubbingsDescending()) { if (container.getInvocationForStubbing().matches(stubbing.getInvocation())) { return stubbing.answer(invocation); } } // record deep stub answer StubbedInvocationMatcher stubbing = recordDeepStubAnswer( newDeepStubMock(returnTypeGenericMetadata, invocation.getMock()), container ); // deep stubbing creates a stubbing and immediately uses it // so the stubbing is actually used by the same invocation stubbing.markStubUsed(stubbing.getInvocation()); return stubbing.answer(invocation); }