public void verify(VerificationData verificationData) { List<Invocation> invocations = verificationData.getAllInvocations(); InvocationMatcher invocationMatcher = verificationData.getWanted(); if (invocations == null || invocations.isEmpty()) { throw new MockitoException( "\nNo interactions with " + invocationMatcher.getInvocation().getMock() + " mock so far"); } Invocation invocation = invocations.get(invocations.size() - 1); if (!invocationMatcher.matches(invocation)) { throw new MockitoException("\nWanted but not invoked:\n" + invocationMatcher); } }
@Test public void shouldGetResultsForMethods() throws Throwable { invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod)); invocationContainerImpl.addAnswer(new Returns("simpleMethod")); Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation(); invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod)); invocationContainerImpl.addAnswer(new ThrowsException(new MyException())); assertEquals("simpleMethod", invocationContainerImpl.answerTo(simpleMethod)); try { invocationContainerImpl.answerTo(differentMethod); fail(); } catch (MyException e) {} }
@Test public void should_get_results_for_methods() throws Throwable { invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod)); invocationContainerImpl.addAnswer(new Returns("simpleMethod")); Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation(); invocationContainerImpl.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod)); invocationContainerImpl.addAnswer(new ThrowsException(new MyException())); assertEquals("simpleMethod", invocationContainerImpl.answerTo(simpleMethod)); try { invocationContainerImpl.answerTo(differentMethod); fail(); } catch (MyException e) {} }
@Test public void should_get_results_for_methods_stub_only() throws Throwable { invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(simpleMethod)); invocationContainerImplStubOnly.addAnswer(new Returns("simpleMethod")); Invocation differentMethod = new InvocationBuilder().differentMethod().toInvocation(); invocationContainerImplStubOnly.setInvocationForPotentialStubbing(new InvocationMatcher(differentMethod)); invocationContainerImplStubOnly.addAnswer(new ThrowsException(new MyException())); assertEquals("simpleMethod", invocationContainerImplStubOnly.answerTo(simpleMethod)); try { invocationContainerImplStubOnly.answerTo(differentMethod); fail(); } catch (MyException e) {} }
@Test public void shouldRemoveVerificationModeEvenWhenInvalidMatchers() throws Throwable { // given Invocation invocation = new InvocationBuilder().toInvocation(); @SuppressWarnings("rawtypes") MockHandlerImpl<?> handler = new MockHandlerImpl(new MockSettingsImpl()); handler.mockingProgress.verificationStarted(VerificationModeFactory.atLeastOnce()); handler.matchersBinder = new MatchersBinder() { public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, Invocation invocation) { throw new InvalidUseOfMatchersException(); } }; try { // when handler.handle(invocation); // then fail(); } catch (InvalidUseOfMatchersException e) { } assertNull(handler.mockingProgress.pullVerificationMode()); }
public InvocationMatcher bindMatchers(ArgumentMatcherStorage argumentMatcherStorage, final Invocation invocation) { List<LocalizedMatcher> lastMatchers = argumentMatcherStorage.pullLocalizedMatchers(); validateMatchers(invocation, lastMatchers); final InvocationMatcher invocationWithMatchers = new InvocationMatcher(invocation, (List<Matcher>)(List) lastMatchers) { @Override public String toString() { return invocation.toString(); } }; return invocationWithMatchers; }
public void setMethodForStubbing(InvocationMatcher invocation) { invocationForStubbing = invocation; assert hasAnswersForStubbing(); for (int i = 0; i < answersForStubbing.size(); i++) { addAnswer(answersForStubbing.get(i), i != 0); } answersForStubbing.clear(); }
public void verify(VerificationData data) { List<Invocation> invocations = data.getAllInvocations(); InvocationMatcher wanted = data.getWanted(); InvocationsFinder finder = new InvocationsFinder(); List<Invocation> found = finder.findInvocations(invocations, wanted); int foundSize = found.size(); if (foundSize > maxNumberOfInvocations) { new Reporter().wantedAtMostX(maxNumberOfInvocations, foundSize); } invocationMarker.markVerified(found, wanted); }
public void verifyInOrder(VerificationDataInOrder data) { List<Invocation> allInvocations = data.getAllInvocations(); InvocationMatcher wanted = data.getWanted(); MissingInvocationInOrderChecker missingInvocation = new MissingInvocationInOrderChecker(); AtLeastXNumberOfInvocationsInOrderChecker numberOfCalls = new AtLeastXNumberOfInvocationsInOrderChecker(data.getOrderingContext()); if (wantedCount == 1) { missingInvocation.check(allInvocations, wanted, this, data.getOrderingContext()); } numberOfCalls.check(allInvocations, wanted, wantedCount); }
public void verifyInOrder(VerificationDataInOrder data) { List<Invocation> allInvocations = data.getAllInvocations(); InvocationMatcher wanted = data.getWanted(); if (wantedCount > 0) { MissingInvocationInOrderChecker missingInvocation = new MissingInvocationInOrderChecker(); missingInvocation.check(allInvocations, wanted, this, data.getOrderingContext()); } NumberOfInvocationsInOrderChecker numberOfCalls = new NumberOfInvocationsInOrderChecker(); numberOfCalls.check(allInvocations, wanted, wantedCount, data.getOrderingContext()); }
@SuppressWarnings("unchecked") public void verify(VerificationData data) { InvocationMatcher wantedMatcher = data.getWanted(); List<Invocation> invocations = data.getAllInvocations(); List<Invocation> chunk = finder.findInvocations(invocations,wantedMatcher); if (invocations.size() != 1 && chunk.size() > 0) { Invocation unverified = finder.findFirstUnverified(invocations); reporter.noMoreInteractionsWanted(unverified, (List) invocations); } else if (invocations.size() != 1 || chunk.size() == 0) { reporter.wantedButNotInvoked(wantedMatcher); } marker.markVerified(chunk.get(0), wantedMatcher); }
public void check(List<Invocation> invocations, InvocationMatcher wanted) { List<Invocation> actualInvocations = finder.findInvocations(invocations, wanted); if (actualInvocations.isEmpty()) { Invocation similar = finder.findSimilarInvocation(invocations, wanted); if (similar != null) { ArgumentMatchingTool argumentMatchingTool = new ArgumentMatchingTool(); Integer[] indexesOfSuspiciousArgs = argumentMatchingTool.getSuspiciouslyNotMatchingArgsIndexes(wanted.getMatchers(), similar.getArguments()); SmartPrinter smartPrinter = new SmartPrinter(wanted, similar, indexesOfSuspiciousArgs); reporter.argumentsAreDifferent(smartPrinter.getWanted(), smartPrinter.getActual(), similar.getLocation()); } else { reporter.wantedButNotInvoked(wanted, invocations); } } }
public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount, InOrderContext context) { List<Invocation> chunk = finder.findMatchingChunk(invocations, wanted, wantedCount, context); int actualCount = chunk.size(); if (wantedCount > actualCount) { Location lastInvocation = finder.getLastLocation(chunk); reporter.tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastInvocation); } else if (wantedCount < actualCount) { Location firstUndesired = chunk.get(wantedCount).getLocation(); reporter.tooManyActualInvocationsInOrder(wantedCount, actualCount, wanted, firstUndesired); } invocationMarker.markVerifiedInOrder(chunk, wanted, context); }
public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount) { List<Invocation> chunk = finder.findAllMatchingUnverifiedChunks(invocations, wanted, orderingContext); int actualCount = chunk.size(); if (wantedCount > actualCount) { Location lastLocation = finder.getLastLocation(chunk); reporter.tooLittleActualInvocationsInOrder(new AtLeastDiscrepancy(wantedCount, actualCount), wanted, lastLocation); } invocationMarker.markVerifiedInOrder(chunk, wanted, orderingContext); }
public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount) { List<Invocation> actualInvocations = finder.findInvocations(invocations, wanted); int actualCount = actualInvocations.size(); if (wantedCount > actualCount) { Location lastLocation = finder.getLastLocation(actualInvocations); reporter.tooLittleActualInvocations(new AtLeastDiscrepancy(wantedCount, actualCount), wanted, lastLocation); } invocationMarker.markVerified(actualInvocations, wanted); }
public void check(List<Invocation> invocations, InvocationMatcher wanted, VerificationMode mode, InOrderContext context) { List<Invocation> chunk = finder.findAllMatchingUnverifiedChunks(invocations, wanted, context); if (!chunk.isEmpty()) { return; } Invocation previousInOrder = finder.findPreviousVerifiedInOrder(invocations, context); if (previousInOrder == null) { /** * It is of course possible to have an issue where the arguments are different * rather that not invoked in order. Issue related to * http://code.google.com/p/mockito/issues/detail?id=27. If the previous order * is missing, then this method checks if the arguments are different or if the order * is not invoked. */ List<Invocation> actualInvocations = finder.findInvocations(invocations, wanted); if (actualInvocations == null || actualInvocations.isEmpty()) { Invocation similar = finder.findSimilarInvocation(invocations, wanted); if (similar != null) { Integer[] indicesOfSimilarMatchingArguments = new ArgumentMatchingTool().getSuspiciouslyNotMatchingArgsIndexes(wanted.getMatchers(), similar.getArguments()); SmartPrinter smartPrinter = new SmartPrinter(wanted, similar, indicesOfSimilarMatchingArguments); reporter.argumentsAreDifferent(smartPrinter.getWanted(), smartPrinter.getActual(), similar.getLocation()); } else { reporter.wantedButNotInvoked(wanted); } } } else { reporter.wantedButNotInvokedInOrder(wanted, previousInOrder); } }
public String getWarnings() { List<Invocation> unused = new UnusedStubsFinder().find(createdMocks); List<Invocation> all = new AllInvocationsFinder().find(createdMocks); List<InvocationMatcher> allInvocationMatchers = InvocationMatcher.createFrom(all); String warnings = new WarningsPrinterImpl(unused, allInvocationMatchers, false).print(); return warnings; }
public void foundStubCalledWithDifferentArgs(Invocation unused, InvocationMatcher unstubbed) { logger.log(join( " *** Stubbing warnings from Mockito: *** ", "", "stubbed with those args here " + unused.getLocation(), "BUT called with different args " + unstubbed.getInvocation().getLocation(), "")); }
public void foundUnstubbed(InvocationMatcher unstubbed) { if (warnAboutUnstubbed) { logger.log(join( "This method was not stubbed ", unstubbed, unstubbed.getInvocation().getLocation(), "")); } }
@Test public void shouldAddThrowableForVoidMethod() throws Throwable { invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(new MyException())); invocationContainerImpl.setMethodForStubbing(new InvocationMatcher(simpleMethod)); try { invocationContainerImpl.answerTo(simpleMethod); fail(); } catch (MyException e) {} }
@Test public void shouldValidateThrowableForVoidMethod() throws Throwable { invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(new Exception())); try { invocationContainerImpl.setMethodForStubbing(new InvocationMatcher(simpleMethod)); fail(); } catch (MockitoException e) {} }
@Test public void shouldToStringBeNotVerifiable() throws Exception { InvocationMatcher toString = new InvocationBuilder().method("toString").toInvocationMatcher(); try { new VerificationDataImpl(null, toString); fail(); } catch (MockitoException e) {} }
@Test public void shouldMarkAsVerified() { //given Invocation invocation = new InvocationBuilder().toInvocation(); assertFalse(invocation.isVerified()); //when only.verify(new VerificationDataStub(new InvocationMatcher(invocation), invocation)); //then assertTrue(invocation.isVerified()); }
@Test public void shouldPrintUnusedStub() { // given Invocation unusedStub = new InvocationBuilder().simpleMethod().toInvocation(); // when WarningsFinder finder = new WarningsFinder(asList(unusedStub), Arrays.<InvocationMatcher>asList()); finder.find(listener); // then verify(listener, only()).foundUnusedStub(unusedStub); }
@Test public void shouldPrintUnstubbedInvocation() { // given InvocationMatcher unstubbedInvocation = new InvocationBuilder().differentMethod().toInvocationMatcher(); // when WarningsFinder finder = new WarningsFinder(Arrays.<Invocation>asList(), Arrays.<InvocationMatcher>asList(unstubbedInvocation)); finder.find(listener); // then verify(listener, only()).foundUnstubbed(unstubbedInvocation); }
@Test public void shouldPrintStubWasUsedWithDifferentArgs() { // given Invocation stub = new InvocationBuilder().arg("foo").mock(mock).toInvocation(); InvocationMatcher wrongArg = new InvocationBuilder().arg("bar").mock(mock).toInvocationMatcher(); // when WarningsFinder finder = new WarningsFinder(Arrays.<Invocation> asList(stub), Arrays.<InvocationMatcher> asList(wrongArg)); finder.find(listener); // then verify(listener, only()).foundStubCalledWithDifferentArgs(stub, wrongArg); }
public SmartPrinter(InvocationMatcher wanted, Invocation actual, Integer ... indexesOfMatchersToBeDescribedWithExtraTypeInfo) { PrintSettings printSettings = new PrintSettings(); printSettings.setMultiline(wanted.toString().contains("\n") || actual.toString().contains("\n")); printSettings.setMatchersToBeDescribedWithExtraTypeInfo(indexesOfMatchersToBeDescribedWithExtraTypeInfo); this.wanted = printSettings.print(wanted); this.actual = printSettings.print(actual); }
private VerificationDataImpl createVerificationData(InvocationContainerImpl invocationContainerImpl, InvocationMatcher invocationMatcher) { if (mockSettings.isStubOnly()) { new Reporter().stubPassedToVerify(); // this throws an exception } return new VerificationDataImpl(invocationContainerImpl, invocationMatcher); }
public void verifyInOrder(VerificationDataInOrder data) { List<Invocation> allInvocations = data.getAllInvocations(); InvocationMatcher wanted = data.getWanted(); MissingInvocationInOrderChecker missingInvocation = new MissingInvocationInOrderChecker(); missingInvocation.check( allInvocations, wanted, this, data.getOrderingContext()); NonGreedyNumberOfInvocationsInOrderChecker numberOfCalls = new NonGreedyNumberOfInvocationsInOrderChecker(); numberOfCalls.check( allInvocations, wanted, wantedCount, data.getOrderingContext()); }
public void check(List<Invocation> invocations, InvocationMatcher wanted, int wantedCount, InOrderContext context) { int actualCount = 0; Location lastLocation = null; while( actualCount < wantedCount ){ Invocation next = finder.findFirstMatchingUnverifiedInvocation( invocations, wanted, context ); if( next == null ){ reporter.tooLittleActualInvocationsInOrder(new Discrepancy(wantedCount, actualCount), wanted, lastLocation ); } marker.markVerified( next, wanted ); context.markVerified( next ); lastLocation = next.getLocation(); actualCount++; } }