org.mockito.internal.invocation.InvocationMatcher Java Examples

The following examples show how to use org.mockito.internal.invocation.InvocationMatcher. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ConcurrentCompositeLineConsumerTest.java    From che with Eclipse Public License 2.0 6 votes vote down vote up
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);
  }
}
 
Example #2
Source File: MockitoStubberTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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) {}
}
 
Example #3
Source File: InvocationContainerImplStubbingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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) {}
}
 
Example #4
Source File: InvocationContainerImplStubbingTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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) {}
}
 
Example #5
Source File: MockHandlerImplTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@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());
}
 
Example #6
Source File: OnlyTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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());
}
 
Example #7
Source File: NonGreedyNumberOfInvocationsInOrderChecker.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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++;
    }
}
 
Example #8
Source File: InvocationContainerImplStubbingTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_add_throwable_for_void_method() throws Throwable {
    invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(new MyException()));
    invocationContainerImpl.setMethodForStubbing(new InvocationMatcher(simpleMethod));

    try {
        invocationContainerImpl.answerTo(simpleMethod);
        fail();
    } catch (MyException e) {}
}
 
Example #9
Source File: LoggingListener.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void foundUnstubbed(InvocationMatcher unstubbed) {
    if (warnAboutUnstubbed) {
        logger.log(join(
                "This method was not stubbed ",
                unstubbed,
                unstubbed.getInvocation().getLocation(),
                ""));
    }
}
 
Example #10
Source File: LoggingListener.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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(),
            ""));
}
 
Example #11
Source File: LastInteraction.java    From Volley-Ball with MIT License 5 votes vote down vote up
@Override
public void verify(VerificationData data) {
    List<Invocation> invocations = data.getAllInvocations();
    InvocationMatcher matcher = data.getWanted();
    Invocation invocation = invocations.get(invocations.size() - 1);

    if (!matcher.matches(invocation)) {
        throw new MockitoException("This is not the last interaction with the mock object");
    }
}
 
Example #12
Source File: MissingInvocationInOrderChecker.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
    }
}
 
Example #13
Source File: MissingInvocationChecker.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
        }
    }
}
 
Example #14
Source File: Times.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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());
}
 
Example #15
Source File: Only.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #16
Source File: AtLeast.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #17
Source File: NumberOfInvocationsInOrderChecker.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #18
Source File: VerificationDataImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldToStringBeNotVerifiable() throws Exception {
    InvocationMatcher toString = new InvocationBuilder().method("toString").toInvocationMatcher();
    try {
        new VerificationDataImpl(null, toString);
        fail();
    } catch (MockitoException e) {}
}
 
Example #19
Source File: AtLeastXNumberOfInvocationsChecker.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #20
Source File: Calls.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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());
}
 
Example #21
Source File: AtMost.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #22
Source File: WarningsFinderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #23
Source File: InvocationContainerImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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();
}
 
Example #24
Source File: SmartPrinter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
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);
}
 
Example #25
Source File: WarningsFinderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #26
Source File: WarningsFinderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #27
Source File: WarningsFinderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #28
Source File: WarningsFinderTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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);
}
 
Example #29
Source File: OnlyTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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());
}
 
Example #30
Source File: VerificationDataImplTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldToStringBeNotVerifiable() throws Exception {
    InvocationMatcher toString = new InvocationBuilder().method("toString").toInvocationMatcher();
    try {
        new VerificationDataImpl(null, toString);
        fail();
    } catch (MockitoException e) {}
}