Search in sources :

Example 11 with MockitoAssertionError

use of org.mockito.exceptions.base.MockitoAssertionError in project mockito by mockito.

the class DescriptionTest method verification_failure_should_prepend_expected_message.

/**
     * Test of verify method, of class Description. This test validates that the custom message is prepended to the
     * error message when verification fails.
     */
@Test
public void verification_failure_should_prepend_expected_message() {
    String failureMessage = "message should be prepended to the original message";
    String exceptionMessage = "original error message";
    String expectedResult = failureMessage + "\n" + exceptionMessage;
    MockitoAssertionError error = new MockitoAssertionError(exceptionMessage);
    doThrow(error).when(mockVerificationMode).verify(mockVerificationData);
    Description instance = new Description(mockVerificationMode, failureMessage);
    try {
        instance.verify(mockVerificationData);
        verify(mockVerificationMode).verify(mockVerificationData);
        fail("Should not have made it this far");
    } catch (MockitoAssertionError e) {
        assertEquals(expectedResult, e.getMessage());
    }
}
Also used : MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Test(org.junit.Test)

Example 12 with MockitoAssertionError

use of org.mockito.exceptions.base.MockitoAssertionError in project mockito by mockito.

the class VerificationOverTimeImplTest method should_throw_mockito_assertion_error.

@Test
public void should_throw_mockito_assertion_error() {
    MockitoAssertionError toBeThrown = new MockitoAssertionError("message");
    exception.expect(is(toBeThrown));
    doThrow(toBeThrown).when(delegate).verify(null);
    impl.verify(null);
}
Also used : MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Test(org.junit.Test)

Example 13 with MockitoAssertionError

use of org.mockito.exceptions.base.MockitoAssertionError in project powermock by powermock.

the class MockitoMethodInvocationControl method performIntercept.

private Object performIntercept(MethodInterceptorFilter invocationHandler, final Object interceptionObject, final Method method, Object[] arguments) throws Throwable {
    MockHandler mockHandler = invocationHandler.getHandler();
    final CleanTraceRealMethod cglibProxyRealMethod = new CleanTraceRealMethod(new RealMethod() {

        private static final long serialVersionUID = 4564320968038564170L;

        @Override
        public Object invoke(Object target, Object[] arguments) throws Throwable {
            /*
                     * Instruct the MockGateway to don't intercept the next call.
                     * The reason is that when Mockito is spying on objects it
                     * should call the "real method" (which is proxied by Mockito
                     * anyways) so that we don't end up in here one more time which
                     * causes infinite recursion. This should not be done if the
                     * interceptionObject is a final system class because these are
                     * never caught by the Mockito proxy.
                     */
            final Class<?> type = Whitebox.getType(interceptionObject);
            final boolean isFinalSystemClass = type.getName().startsWith("java.") && Modifier.isFinal(type.getModifiers());
            if (!isFinalSystemClass) {
                MockRepository.putAdditionalState(MockGateway.DONT_MOCK_NEXT_CALL, true);
            }
            try {
                return method.invoke(target, arguments);
            } catch (InvocationTargetException e) {
                SafeExceptionRethrower.safeRethrow(e.getCause());
            }
            return null;
        }
    });
    Invocation invocation = new InvocationImpl(interceptionObject, new DelegatingMethod(method), arguments, SequenceNumber.next(), cglibProxyRealMethod, new LocationImpl()) {

        private static final long serialVersionUID = -3679957412502758558L;

        @Override
        public String toString() {
            return new ToStringGenerator().generate(getMock(), getMethod(), getArguments());
        }
    };
    try {
        return replaceMatchersBinderIfNeeded(mockHandler).handle(invocation);
    } catch (NotAMockException e) {
        if (invocation.getMock().getClass().getName().startsWith("java.") && MockRepository.getInstanceMethodInvocationControl(invocation.getMock()) != null) {
            return invocation.callRealMethod();
        } else {
            throw e;
        }
    } catch (MockitoAssertionError e) {
        InvocationControlAssertionError.updateErrorMessageForMethodInvocation(e);
        throw e;
    }
}
Also used : MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Invocation(org.mockito.invocation.Invocation) NotAMockException(org.mockito.exceptions.misusing.NotAMockException) InvocationImpl(org.mockito.internal.invocation.InvocationImpl) InvocationTargetException(java.lang.reflect.InvocationTargetException) CleanTraceRealMethod(org.mockito.internal.invocation.realmethod.CleanTraceRealMethod) DelegatingMethod(org.mockito.internal.creation.DelegatingMethod) LocationImpl(org.mockito.internal.debugging.LocationImpl) InternalMockHandler(org.mockito.internal.InternalMockHandler) MockHandler(org.mockito.invocation.MockHandler) RealMethod(org.mockito.internal.invocation.realmethod.RealMethod) CleanTraceRealMethod(org.mockito.internal.invocation.realmethod.CleanTraceRealMethod)

Aggregations

MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)13 Test (org.junit.Test)9 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)5 PrivateMethodDemo (samples.privatemocking.PrivateMethodDemo)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 NotAMockException (org.mockito.exceptions.misusing.NotAMockException)2 InternalMockHandler (org.mockito.internal.InternalMockHandler)2 Invocation (org.mockito.invocation.Invocation)2 MockHandler (org.mockito.invocation.MockHandler)2 DelegatingMethod (org.mockito.internal.creation.DelegatingMethod)1 LocationImpl (org.mockito.internal.debugging.LocationImpl)1 InvocationBuilder (org.mockito.internal.invocation.InvocationBuilder)1 InvocationImpl (org.mockito.internal.invocation.InvocationImpl)1 CleanTraceRealMethod (org.mockito.internal.invocation.realmethod.CleanTraceRealMethod)1 RealMethod (org.mockito.internal.invocation.realmethod.RealMethod)1 InvocationContainer (org.mockito.internal.stubbing.InvocationContainer)1 VerificationDataImpl (org.mockito.internal.verification.VerificationDataImpl)1 MatchableInvocation (org.mockito.invocation.MatchableInvocation)1 VerificationCollector (org.mockito.junit.VerificationCollector)1 IMethods (org.mockitousage.IMethods)1