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]");
}
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]");
}
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);
}
}
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;
}
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;
}
}
Aggregations