类org.mockito.internal.invocation.InvocationMatcher源码实例Demo

下面列出了怎么用org.mockito.internal.invocation.InvocationMatcher的API类实例代码及写法,或者点击链接到github查看源代码。

源代码1 项目: che   文件: ConcurrentCompositeLineConsumerTest.java
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);
  }
}
 
源代码2 项目: astor   文件: MockitoStubberTest.java
@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) {}
}
 
源代码3 项目: astor   文件: MockHandlerImplTest.java
@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());
}
 
@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) {}
}
 
源代码6 项目: astor   文件: VerificationDataImplTest.java
@Test
public void shouldToStringBeNotVerifiable() throws Exception {
    InvocationMatcher toString = new InvocationBuilder().method("toString").toInvocationMatcher();
    try {
        new VerificationDataImpl(null, toString);
        fail();
    } catch (MockitoException e) {}
}
 
源代码7 项目: astor   文件: InvocationContainerImpl.java
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();
}
 
源代码8 项目: astor   文件: AtMost.java
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);
}
 
源代码9 项目: astor   文件: AtLeast.java
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);
}
 
源代码10 项目: astor   文件: Times.java
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());
}
 
源代码11 项目: astor   文件: Only.java
@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);
}
 
源代码12 项目: astor   文件: MissingInvocationChecker.java
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);
        }
    }
}
 
源代码13 项目: astor   文件: NumberOfInvocationsInOrderChecker.java
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);
}
 
源代码15 项目: astor   文件: MissingInvocationInOrderChecker.java
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);
    }
}
 
源代码16 项目: astor   文件: WarningsCollector.java
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;
}
 
源代码17 项目: astor   文件: LoggingListener.java
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(),
            ""));
}
 
源代码18 项目: astor   文件: LoggingListener.java
public void foundUnstubbed(InvocationMatcher unstubbed) {
    if (warnAboutUnstubbed) {
        logger.log(join(
                "This method was not stubbed ",
                unstubbed,
                unstubbed.getInvocation().getLocation(),
                ""));
    }
}
 
源代码19 项目: astor   文件: MockitoStubberTest.java
@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) {}
}
 
源代码20 项目: astor   文件: MockitoStubberTest.java
@Test
public void shouldValidateThrowableForVoidMethod() throws Throwable {
    invocationContainerImpl.addAnswerForVoidMethod(new ThrowsException(new Exception()));
    
    try {
        invocationContainerImpl.setMethodForStubbing(new InvocationMatcher(simpleMethod));
        fail();
    } catch (MockitoException e) {}
}
 
源代码21 项目: astor   文件: VerificationDataImplTest.java
@Test
public void shouldToStringBeNotVerifiable() throws Exception {
    InvocationMatcher toString = new InvocationBuilder().method("toString").toInvocationMatcher();
    try {
        new VerificationDataImpl(null, toString);
        fail();
    } catch (MockitoException e) {}
}
 
源代码22 项目: astor   文件: OnlyTest.java
@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());
}
 
源代码23 项目: astor   文件: WarningsFinderTest.java
@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);
}
 
源代码24 项目: astor   文件: WarningsFinderTest.java
@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);
}
 
源代码25 项目: astor   文件: WarningsFinderTest.java
@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);
}
 
源代码26 项目: astor   文件: WarningsFinderTest.java
@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);
}
 
源代码27 项目: astor   文件: SmartPrinter.java
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);
}
 
源代码28 项目: astor   文件: InvocationContainerImpl.java
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();
}
 
源代码29 项目: astor   文件: WarningsFinderTest.java
@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);
}
 
源代码30 项目: astor   文件: AtMost.java
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);
}
 
 类所在包
 类方法
 同包方法