Search in sources :

Example 1 with VerificationDataImpl

use of org.mockito.internal.verification.VerificationDataImpl in project powermock by powermock.

the class MockitoMethodInvocationControl method verifyNoMoreInteractions.

public void verifyNoMoreInteractions() {
    try {
        final MockHandler mockHandler = methodInterceptorFilter.getHandler();
        if (mockHandler instanceof MockHandler) {
            InvocationContainer invocationContainer = Whitebox.invokeMethod(mockHandler, "getInvocationContainer");
            VerificationDataImpl data = new VerificationDataImpl(invocationContainer, null);
            VerificationModeFactory.noMoreInteractions().verify(data);
        } else {
            throw new RuntimeException("Cannot perform verifyNoMoreInteractions because of unknown mockhandler type " + mockHandler.getClass());
        }
    } catch (MockitoAssertionError e) {
        InvocationControlAssertionError.updateErrorMessageForVerifyNoMoreInteractions(e);
        throw e;
    } catch (Exception e) {
        throw new RuntimeException("PowerMock internal error", e);
    }
}
Also used : VerificationDataImpl(org.mockito.internal.verification.VerificationDataImpl) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) InternalMockHandler(org.mockito.internal.InternalMockHandler) MockHandler(org.mockito.invocation.MockHandler) InvocationContainer(org.mockito.internal.stubbing.InvocationContainer) NotAMockException(org.mockito.exceptions.misusing.NotAMockException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with VerificationDataImpl

use of org.mockito.internal.verification.VerificationDataImpl in project mockito by mockito.

the class MockitoCore method verifyNoMoreInteractions.

public void verifyNoMoreInteractions(Object... mocks) {
    assertMocksNotEmpty(mocks);
    mockingProgress().validateState();
    for (Object mock : mocks) {
        try {
            if (mock == null) {
                throw nullPassedToVerifyNoMoreInteractions();
            }
            InvocationContainerImpl invocations = getInvocationContainer(mock);
            assertNotStubOnlyMock(mock);
            VerificationDataImpl data = new VerificationDataImpl(invocations, null);
            noMoreInteractions().verify(data);
        } catch (NotAMockException e) {
            throw notAMockPassedToVerifyNoMoreInteractions();
        }
    }
}
Also used : VerificationDataImpl(org.mockito.internal.verification.VerificationDataImpl) InvocationContainerImpl(org.mockito.internal.stubbing.InvocationContainerImpl) NotAMockException(org.mockito.exceptions.misusing.NotAMockException)

Example 3 with VerificationDataImpl

use of org.mockito.internal.verification.VerificationDataImpl 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 4 with VerificationDataImpl

use of org.mockito.internal.verification.VerificationDataImpl in project mockito by mockito.

the class MockitoCore method verifyNoInteractions.

public void verifyNoInteractions(Object... mocks) {
    assertMocksNotEmpty(mocks);
    mockingProgress().validateState();
    for (Object mock : mocks) {
        try {
            if (mock == null) {
                throw nullPassedToVerifyNoMoreInteractions();
            }
            InvocationContainerImpl invocations = getInvocationContainer(mock);
            assertNotStubOnlyMock(mock);
            VerificationDataImpl data = new VerificationDataImpl(invocations, null);
            noInteractions().verify(data);
        } catch (NotAMockException e) {
            throw notAMockPassedToVerifyNoMoreInteractions();
        }
    }
}
Also used : VerificationDataImpl(org.mockito.internal.verification.VerificationDataImpl) InvocationContainerImpl(org.mockito.internal.stubbing.InvocationContainerImpl) NotAMockException(org.mockito.exceptions.misusing.NotAMockException)

Example 5 with VerificationDataImpl

use of org.mockito.internal.verification.VerificationDataImpl in project mockito by mockito.

the class MockedStaticImpl method verifyNoInteractions.

@Override
public void verifyNoInteractions() {
    assertNotClosed();
    mockingProgress().validateState();
    InvocationContainerImpl invocations = getInvocationContainer(control.getType());
    VerificationDataImpl data = new VerificationDataImpl(invocations, null);
    noInteractions().verify(data);
}
Also used : VerificationDataImpl(org.mockito.internal.verification.VerificationDataImpl) InvocationContainerImpl(org.mockito.internal.stubbing.InvocationContainerImpl)

Aggregations

VerificationDataImpl (org.mockito.internal.verification.VerificationDataImpl)6 InvocationContainerImpl (org.mockito.internal.stubbing.InvocationContainerImpl)4 NotAMockException (org.mockito.exceptions.misusing.NotAMockException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)1 InternalMockHandler (org.mockito.internal.InternalMockHandler)1 InvocationMatcher (org.mockito.internal.invocation.InvocationMatcher)1 InvocationContainer (org.mockito.internal.stubbing.InvocationContainer)1 OngoingStubbingImpl (org.mockito.internal.stubbing.OngoingStubbingImpl)1 StubbedInvocationMatcher (org.mockito.internal.stubbing.StubbedInvocationMatcher)1 MockAwareVerificationMode (org.mockito.internal.verification.MockAwareVerificationMode)1 MockHandler (org.mockito.invocation.MockHandler)1 VerificationMode (org.mockito.verification.VerificationMode)1