Search in sources :

Example 1 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher in project mockito by mockito.

the class WarningsCollector method getWarnings.

public String getWarnings() {
    List<Invocation> unused = new UnusedStubsFinder().find(createdMocks);
    List<Invocation> all = AllInvocationsFinder.find(createdMocks);
    List<InvocationMatcher> allInvocationMatchers = InvocationMatcher.createFrom(all);
    return new WarningsPrinterImpl(unused, allInvocationMatchers, false).print();
}
Also used : UnusedStubsFinder(org.mockito.internal.invocation.UnusedStubsFinder) Invocation(org.mockito.invocation.Invocation) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher)

Example 2 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher in project mockito by mockito.

the class AtLeastXNumberOfInvocationsCheckerTest method shouldReportTooLittleInvocationsInOrder.

@Test
public void shouldReportTooLittleInvocationsInOrder() {
    InOrderContext context = new InOrderContextImpl();
    //given
    Invocation invocation = new InvocationBuilder().simpleMethod().toInvocation();
    Invocation invocationTwo = new InvocationBuilder().differentMethod().toInvocation();
    exception.expect(VerificationInOrderFailure.class);
    exception.expectMessage("iMethods.simpleMethod()");
    exception.expectMessage("Wanted *at least* 2 times");
    exception.expectMessage("But was 1 time");
    //when
    checkAtLeastNumberOfInvocations(asList(invocation, invocationTwo), new InvocationMatcher(invocation), 2, context);
}
Also used : InOrderContextImpl(org.mockito.internal.verification.InOrderContextImpl) Invocation(org.mockito.invocation.Invocation) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) InOrderContext(org.mockito.internal.verification.api.InOrderContext) Test(org.junit.Test)

Example 3 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher in project mockito by mockito.

the class WarningsFinderTest method shouldPrintUnstubbedInvocation.

@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);
}
Also used : InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 4 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher in project mockito by mockito.

the class MockHandlerImpl method handle.

@Override
public Object handle(Invocation invocation) throws Throwable {
    if (invocationContainer.hasAnswersForStubbing()) {
        // stubbing voids with doThrow() or doAnswer() style
        InvocationMatcher invocationMatcher = matchersBinder.bindMatchers(mockingProgress().getArgumentMatcherStorage(), invocation);
        invocationContainer.setMethodForStubbing(invocationMatcher);
        return null;
    }
    VerificationMode verificationMode = mockingProgress().pullVerificationMode();
    InvocationMatcher invocationMatcher = matchersBinder.bindMatchers(mockingProgress().getArgumentMatcherStorage(), invocation);
    mockingProgress().validateState();
    // if verificationMode is not null then someone is doing verify()
    if (verificationMode != null) {
        // - see VerifyingWithAnExtraCallToADifferentMockTest (bug 138)
        if (MockUtil.areSameMocks(((MockAwareVerificationMode) verificationMode).getMock(), invocation.getMock())) {
            VerificationDataImpl data = new VerificationDataImpl(invocationContainer, invocationMatcher);
            verificationMode.verify(data);
            return null;
        } else {
            // this means there is an invocation on a different mock. Re-adding verification
            // mode
            // - see VerifyingWithAnExtraCallToADifferentMockTest (bug 138)
            mockingProgress().verificationStarted(verificationMode);
        }
    }
    // prepare invocation for stubbing
    invocationContainer.setInvocationForPotentialStubbing(invocationMatcher);
    OngoingStubbingImpl<T> ongoingStubbing = new OngoingStubbingImpl<T>(invocationContainer);
    mockingProgress().reportOngoingStubbing(ongoingStubbing);
    // look for existing answer for this invocation
    StubbedInvocationMatcher stubbing = invocationContainer.findAnswerFor(invocation);
    // TODO #793 - when completed, we should be able to get rid of the casting below
    notifyStubbedAnswerLookup(invocation, stubbing, invocationContainer.getStubbingsAscending(), (CreationSettings) mockSettings);
    if (stubbing != null) {
        stubbing.captureArgumentsFrom(invocation);
        try {
            return stubbing.answer(invocation);
        } finally {
            // Needed so that we correctly isolate stubbings in some scenarios
            // see MockitoStubbedCallInAnswerTest or issue #1279
            mockingProgress().reportOngoingStubbing(ongoingStubbing);
        }
    } else {
        Object ret = mockSettings.getDefaultAnswer().answer(invocation);
        DefaultAnswerValidator.validateReturnValueFor(invocation, ret);
        // Mockito uses it to redo setting invocation for potential stubbing in case of partial
        // mocks / spies.
        // Without it, the real method inside 'when' might have delegated to other self method
        // and overwrite the intended stubbed method with a different one.
        // This means we would be stubbing a wrong method.
        // Typically this would led to runtime exception that validates return type with stubbed
        // method signature.
        invocationContainer.resetInvocationForPotentialStubbing(invocationMatcher);
        return ret;
    }
}
Also used : VerificationDataImpl(org.mockito.internal.verification.VerificationDataImpl) StubbedInvocationMatcher(org.mockito.internal.stubbing.StubbedInvocationMatcher) OngoingStubbingImpl(org.mockito.internal.stubbing.OngoingStubbingImpl) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) StubbedInvocationMatcher(org.mockito.internal.stubbing.StubbedInvocationMatcher) MockAwareVerificationMode(org.mockito.internal.verification.MockAwareVerificationMode) VerificationMode(org.mockito.verification.VerificationMode)

Example 5 with InvocationMatcher

use of org.mockito.internal.invocation.InvocationMatcher in project mockito by mockito.

the class NoInteractionsTest method noInteractionsExceptionMessageShouldDescribeMock.

@Test
public void noInteractionsExceptionMessageShouldDescribeMock() {
    // given
    NoInteractions n = new NoInteractions();
    IMethods mock = mock(IMethods.class, "a mock");
    InvocationMatcher i = new InvocationBuilder().mock(mock).toInvocationMatcher();
    InvocationContainerImpl invocations = new InvocationContainerImpl(new MockSettingsImpl());
    invocations.setInvocationForPotentialStubbing(i);
    try {
        // when
        n.verify(new VerificationDataImpl(invocations, null));
        // then
        fail();
    } catch (NoInteractionsWanted e) {
        Assertions.assertThat(e.toString()).contains(mock.toString());
    }
}
Also used : InvocationContainerImpl(org.mockito.internal.stubbing.InvocationContainerImpl) MockSettingsImpl(org.mockito.internal.creation.MockSettingsImpl) NoInteractionsWanted(org.mockito.exceptions.verification.NoInteractionsWanted) InvocationMatcher(org.mockito.internal.invocation.InvocationMatcher) IMethods(org.mockitousage.IMethods) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Aggregations

InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)25 Test (org.junit.Test)19 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)15 Invocation (org.mockito.invocation.Invocation)13 ThrowsException (org.mockito.internal.stubbing.answers.ThrowsException)4 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)3 Returns (org.mockito.internal.stubbing.answers.Returns)3 InOrderContextImpl (org.mockito.internal.verification.InOrderContextImpl)3 InOrderContext (org.mockito.internal.verification.api.InOrderContext)3 MockitoException (org.mockito.exceptions.base.MockitoException)2 NoInteractionsWanted (org.mockito.exceptions.verification.NoInteractionsWanted)2 InvocationContainerImpl (org.mockito.internal.stubbing.InvocationContainerImpl)2 IMethods (org.mockitousage.IMethods)2 LinkedList (java.util.LinkedList)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Before (org.junit.Before)1 ArgumentMatcher (org.mockito.ArgumentMatcher)1 InvalidUseOfMatchersException (org.mockito.exceptions.misusing.InvalidUseOfMatchersException)1 TooFewActualInvocations (org.mockito.exceptions.verification.TooFewActualInvocations)1 VerificationInOrderFailure (org.mockito.exceptions.verification.VerificationInOrderFailure)1