Search in sources :

Example 1 with InvocationContainerImpl

use of org.mockito.internal.stubbing.InvocationContainerImpl 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 2 with InvocationContainerImpl

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

the class NoMoreInteractionsTest method noMoreInteractionsExceptionMessageShouldDescribeMock.

@Test
public void noMoreInteractionsExceptionMessageShouldDescribeMock() {
    //given
    NoMoreInteractions n = new NoMoreInteractions();
    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

InvocationContainerImpl (org.mockito.internal.stubbing.InvocationContainerImpl)2 Test (org.junit.Test)1 NoInteractionsWanted (org.mockito.exceptions.verification.NoInteractionsWanted)1 MockSettingsImpl (org.mockito.internal.creation.MockSettingsImpl)1 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)1 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)1 StubbedInvocationMatcher (org.mockito.internal.stubbing.StubbedInvocationMatcher)1 IMethods (org.mockitousage.IMethods)1