Search in sources :

Example 1 with MockitoAssertionError

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

the class VerificationOverTimeImpl method verify.

/**
     * Verify the given ongoing verification data, and confirm that it satisfies the delegate verification mode
     * before the full duration has passed.
     *
     * In practice, this polls the delegate verification mode until it is satisfied. If it is not satisfied once
     * the full duration has passed, the last error returned by the delegate verification mode will be thrown
     * here in turn. This may be thrown early if the delegate is unsatisfied and the verification mode is known
     * to never recover from this situation (e.g. {@link AtMost}).
     *
     * If it is satisfied before the full duration has passed, behaviour is dependent on the returnOnSuccess parameter
     * given in the constructor. If true, this verification mode is immediately satisfied once the delegate is. If
     * false, this verification mode is not satisfied until the delegate is satisfied and the full time has passed.
     *
     * @throws MockitoAssertionError if the delegate verification mode does not succeed before the timeout
     */
public void verify(VerificationData data) {
    AssertionError error = null;
    timer.start();
    while (timer.isCounting()) {
        try {
            delegate.verify(data);
            if (returnOnSuccess) {
                return;
            } else {
                error = null;
            }
        } catch (MockitoAssertionError e) {
            error = handleVerifyException(e);
        } catch (AssertionError e) {
            error = handleVerifyException(e);
        }
    }
    if (error != null) {
        throw error;
    }
}
Also used : MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError)

Example 2 with MockitoAssertionError

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

the class OnlyTest method shouldNotMarkAsVerifiedWhenAssertionFailed.

@Test
public void shouldNotMarkAsVerifiedWhenAssertionFailed() {
    //given
    Invocation invocation = new InvocationBuilder().toInvocation();
    assertFalse(invocation.isVerified());
    //when
    try {
        only.verify(new VerificationDataStub(new InvocationBuilder().toInvocationMatcher(), invocation));
        fail();
    } catch (MockitoAssertionError e) {
    }
    //then
    assertFalse(invocation.isVerified());
}
Also used : MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Invocation(org.mockito.invocation.Invocation) MatchableInvocation(org.mockito.invocation.MatchableInvocation) InvocationBuilder(org.mockito.internal.invocation.InvocationBuilder) Test(org.junit.Test)

Example 3 with MockitoAssertionError

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

the class VerificationCollectorImplTest method should_collect_multiple_verification_failures.

@Test
public void should_collect_multiple_verification_failures() {
    VerificationCollector collector = MockitoJUnit.collector().assertLazily();
    IMethods methods = mock(IMethods.class);
    verify(methods).simpleMethod();
    verify(methods).byteReturningMethod();
    try {
        collector.collectAndReport();
        fail();
    } catch (MockitoAssertionError error) {
        assertThat(error).hasMessageContaining("1. Wanted but not invoked:");
        assertThat(error).hasMessageContaining("2. Wanted but not invoked:");
    }
}
Also used : VerificationCollector(org.mockito.junit.VerificationCollector) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) IMethods(org.mockitousage.IMethods) Test(org.junit.Test)

Example 4 with MockitoAssertionError

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

the class VerifyNoMoreInteractionsTest method verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreInteractionsTookPlace.

@Test
public void verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreInteractionsTookPlace() throws Exception {
    ExpectNewDemo tested = new ExpectNewDemo();
    MyClass myClassMock = mock(MyClass.class);
    whenNew(MyClass.class).withNoArguments().thenReturn(myClassMock);
    tested.simpleMultipleNew();
    try {
        verifyNoMoreInteractions(MyClass.class);
        fail("Should throw exception!");
    } catch (MockitoAssertionError e) {
        assertTrue(e.getMessage().startsWith("\nNo interactions wanted here:\n-> at samples.powermockito.junit4.verifynomoreinteractions.VerifyNoMoreInteractionsTest.verifyNoMoreInteractionsOnNewInstancesThrowsAssertionErrorWhenMoreInteractionsTookPlace(VerifyNoMoreInteractionsTest.java:"));
    }
}
Also used : ExpectNewDemo(samples.expectnew.ExpectNewDemo) MyClass(samples.newmocking.MyClass) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with MockitoAssertionError

use of org.mockito.exceptions.base.MockitoAssertionError 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)

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