Search in sources :

Example 1 with StubbedInvocationMatcher

use of org.mockito.internal.stubbing.StubbedInvocationMatcher in project mockito by mockito.

the class MockHandlerImpl method handle.

public Object handle(Invocation invocation) throws Throwable {
    if (invocationContainerImpl.hasAnswersForStubbing()) {
        // stubbing voids with doThrow() or doAnswer() style
        InvocationMatcher invocationMatcher = matchersBinder.bindMatchers(mockingProgress().getArgumentMatcherStorage(), invocation);
        invocationContainerImpl.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 (((MockAwareVerificationMode) verificationMode).getMock() == invocation.getMock()) {
            VerificationDataImpl data = createVerificationData(invocationContainerImpl, 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
    invocationContainerImpl.setInvocationForPotentialStubbing(invocationMatcher);
    OngoingStubbingImpl<T> ongoingStubbing = new OngoingStubbingImpl<T>(invocationContainerImpl);
    mockingProgress().reportOngoingStubbing(ongoingStubbing);
    // look for existing answer for this invocation
    StubbedInvocationMatcher stubbedInvocation = invocationContainerImpl.findAnswerFor(invocation);
    notifyStubbedAnswerLookup(invocation, stubbedInvocation);
    if (stubbedInvocation != null) {
        stubbedInvocation.captureArgumentsFrom(invocation);
        return stubbedInvocation.answer(invocation);
    } else {
        Object ret = mockSettings.getDefaultAnswer().answer(invocation);
        DefaultAnswerValidator.validateReturnValueFor(invocation, ret);
        // 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. The reset is required to avoid runtime exception that validates return type with stubbed method signature.
        invocationContainerImpl.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 2 with StubbedInvocationMatcher

use of org.mockito.internal.stubbing.StubbedInvocationMatcher in project mockito by mockito.

the class UnusedStubsFinder method find.

/**
     * 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) {
        InternalMockHandler<Object> handler = MockUtil.getMockHandler(mock);
        List<StubbedInvocationMatcher> fromSingleMock = handler.getInvocationContainer().getStubbedInvocations();
        for (StubbedInvocationMatcher s : fromSingleMock) {
            if (!s.wasUsed()) {
                unused.add(s.getInvocation());
            }
        }
    }
    return unused;
}
Also used : Invocation(org.mockito.invocation.Invocation) StubbedInvocationMatcher(org.mockito.internal.stubbing.StubbedInvocationMatcher)

Example 3 with StubbedInvocationMatcher

use of org.mockito.internal.stubbing.StubbedInvocationMatcher in project mockito by mockito.

the class ReturnsDeepStubs method deepStub.

private Object deepStub(InvocationOnMock invocation, GenericMetadataSupport returnTypeGenericMetadata) throws Throwable {
    InternalMockHandler<Object> handler = MockUtil.getMockHandler(invocation.getMock());
    InvocationContainerImpl container = (InvocationContainerImpl) handler.getInvocationContainer();
    // matches invocation for verification
    for (StubbedInvocationMatcher stubbedInvocationMatcher : container.getStubbedInvocations()) {
        if (container.getInvocationForStubbing().matches(stubbedInvocationMatcher.getInvocation())) {
            return stubbedInvocationMatcher.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);
}
Also used : InvocationContainerImpl(org.mockito.internal.stubbing.InvocationContainerImpl) StubbedInvocationMatcher(org.mockito.internal.stubbing.StubbedInvocationMatcher)

Example 4 with StubbedInvocationMatcher

use of org.mockito.internal.stubbing.StubbedInvocationMatcher in project mockito by mockito.

the class UnusedStubbingsTest method unused_stubbings.

@Test
public void unused_stubbings() throws Exception {
    //given
    UnusedStubbings stubbings = new UnusedStubbings((List) asList(new StubbedInvocationMatcher(new InvocationBuilder().toInvocationMatcher(), doesNothing()), new StubbedInvocationMatcher(new InvocationBuilder().toInvocationMatcher(), doesNothing())));
    //when
    stubbings.format("MyTest.myTestMethod", logger);
    //then
    assertEquals("[MockitoHint] MyTest.myTestMethod (see javadoc for MockitoHint):\n" + "[MockitoHint] 1. Unused -> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n" + "[MockitoHint] 2. Unused -> at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)\n", filterLineNo(logger.getLoggedInfo()));
}
Also used : StubbedInvocationMatcher(org.mockito.internal.stubbing.StubbedInvocationMatcher) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Aggregations

StubbedInvocationMatcher (org.mockito.internal.stubbing.StubbedInvocationMatcher)4 Test (org.junit.Test)1 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)1 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)1 InvocationContainerImpl (org.mockito.internal.stubbing.InvocationContainerImpl)1 OngoingStubbingImpl (org.mockito.internal.stubbing.OngoingStubbingImpl)1 MockAwareVerificationMode (org.mockito.internal.verification.MockAwareVerificationMode)1 VerificationDataImpl (org.mockito.internal.verification.VerificationDataImpl)1 Invocation (org.mockito.invocation.Invocation)1 VerificationMode (org.mockito.verification.VerificationMode)1