/** * 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; } }
/** * Decides if the stubbing should be reported as unused. * Lenient stubbings are not reported as unused. */ public static boolean shouldBeReported(Stubbing stubbing) { return !stubbing.wasUsed() && stubbing.getStrictness() != Strictness.LENIENT; } }
public int compare(Stubbing o1, Stubbing o2) { return invocationComparator.compare(o1.getInvocation(), o2.getInvocation()); } }
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); }
/** * Determines the actual strictness in the following importance order: * 1st - strictness configured when declaring stubbing; * 2nd - strictness configured at mock level; * 3rd - strictness configured at test level (rule, mockito session) * * @param stubbing stubbing to check for strictness. Null permitted. * @param mockSettings settings of the mock object, may or may not have strictness configured. Must not be null. * @param testLevelStrictness strictness configured using the test-level configuration (rule, mockito session). Null permitted. * * @return actual strictness, can be null. */ public static Strictness determineStrictness(Stubbing stubbing, MockCreationSettings mockSettings, Strictness testLevelStrictness) { if (stubbing != null && stubbing.getStrictness() != null) { return stubbing.getStrictness(); } if (mockSettings.isLenient()) { return Strictness.LENIENT; } return testLevelStrictness; } }
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); } }
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 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; }
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; } }
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());