Search in sources :

Example 6 with VerificationMode

use of org.mockito.verification.VerificationMode in project mockito by mockito.

the class VerificationWithTimeoutTest method should_return_formatted_output_from_toString_using_wrapped_verification_mode.

@Test
public void should_return_formatted_output_from_toString_using_wrapped_verification_mode() {
    VerificationMode timeoutAndAtLeastOnce = new Timeout(9, new DummyVerificationMode());
    assertThat(timeoutAndAtLeastOnce).hasToString("Wanted after at most 9 ms: [Dummy verification mode]");
}
Also used : Timeout(org.mockito.verification.Timeout) DummyVerificationMode(org.mockito.internal.verification.DummyVerificationMode) VerificationMode(org.mockito.verification.VerificationMode) DummyVerificationMode(org.mockito.internal.verification.DummyVerificationMode) Test(org.junit.Test)

Example 7 with VerificationMode

use of org.mockito.verification.VerificationMode in project mockito by mockito.

the class VerificationWithTimeoutTest method should_return_formatted_output_from_toString_when_chaining_other_verification_mode.

@Test
public void should_return_formatted_output_from_toString_when_chaining_other_verification_mode() {
    VerificationMode timeoutAndOnly = timeout(7).only();
    assertThat(timeoutAndOnly).hasToString("Wanted after at most 7 ms: [Wanted invocations count: 1 and no other method invoked]");
}
Also used : VerificationMode(org.mockito.verification.VerificationMode) DummyVerificationMode(org.mockito.internal.verification.DummyVerificationMode) Test(org.junit.Test)

Example 8 with VerificationMode

use of org.mockito.verification.VerificationMode in project mockito by mockito.

the class InOrderVerificationTest method shouldThrowExceptionWhenModeIsUnsupported.

@Test
public void shouldThrowExceptionWhenModeIsUnsupported() {
    try (MockedStatic<StaticContext> mockedStatic = mockStatic(StaticContext.class)) {
        // given
        VerificationMode unsupportedMode = data -> {
        };
        InOrder inOrder = inOrder(StaticContext.class);
        // when
        StaticContext.firstMethod();
        // then
        assertThatThrownBy(() -> inOrder.verify(mockedStatic, StaticContext::firstMethod, unsupportedMode)).isInstanceOf(MockitoException.class);
    }
}
Also used : Assert(org.assertj.core.api.Assert) InOrder(org.mockito.InOrder) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) Mockito.mockStatic(org.mockito.Mockito.mockStatic) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) VerificationInOrderFailure(org.mockito.exceptions.verification.VerificationInOrderFailure) MockitoException(org.mockito.exceptions.base.MockitoException) VerificationMode(org.mockito.verification.VerificationMode) Mockito.timeout(org.mockito.Mockito.timeout) MockedStatic(org.mockito.MockedStatic) Assertions.assertThatThrownBy(org.assertj.core.api.Assertions.assertThatThrownBy) Mockito.inOrder(org.mockito.Mockito.inOrder) Assert.fail(org.junit.Assert.fail) NotAMockException(org.mockito.exceptions.misusing.NotAMockException) Mockito.mock(org.mockito.Mockito.mock) InOrder(org.mockito.InOrder) VerificationMode(org.mockito.verification.VerificationMode) Test(org.junit.Test)

Example 9 with VerificationMode

use of org.mockito.verification.VerificationMode in project mockito by mockito.

the class MockingProgressImpl method pullVerificationMode.

@Override
public VerificationMode pullVerificationMode() {
    if (verificationMode == null) {
        return null;
    }
    VerificationMode temp = verificationMode.getObject();
    verificationMode = null;
    return temp;
}
Also used : VerificationMode(org.mockito.verification.VerificationMode)

Example 10 with VerificationMode

use of org.mockito.verification.VerificationMode 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)

Aggregations

VerificationMode (org.mockito.verification.VerificationMode)31 Test (org.junit.Test)18 DummyVerificationMode (org.mockito.internal.verification.DummyVerificationMode)5 MockAwareVerificationMode (org.mockito.internal.verification.MockAwareVerificationMode)3 SnapshotPolicyVO (com.cloud.storage.SnapshotPolicyVO)2 StoragePoolType (com.cloud.storage.Storage.StoragePoolType)2 IntervalType (com.cloud.utils.DateUtil.IntervalType)2 Method (java.lang.reflect.Method)2 Collections.singletonMap (java.util.Collections.singletonMap)2 HashMap (java.util.HashMap)2 LinkedList (java.util.LinkedList)2 Map (java.util.Map)2 AcknowledgeCheckpoint (org.apache.flink.runtime.messages.checkpoint.AcknowledgeCheckpoint)2 DeclineCheckpoint (org.apache.flink.runtime.messages.checkpoint.DeclineCheckpoint)2 OperatorStreamStateHandle (org.apache.flink.runtime.state.OperatorStreamStateHandle)2 PlaceholderStreamStateHandle (org.apache.flink.runtime.state.PlaceholderStreamStateHandle)2 StateHandleID (org.apache.flink.runtime.state.StateHandleID)2 StreamStateHandle (org.apache.flink.runtime.state.StreamStateHandle)2 TestingStreamStateHandle (org.apache.flink.runtime.state.TestingStreamStateHandle)2 ByteStreamStateHandle (org.apache.flink.runtime.state.memory.ByteStreamStateHandle)2