Search in sources :

Example 6 with MockitoAssertionError

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

the class TestUtil method waitUntilVerified.

/**
     * This waits until a mockito verification passed (which is provided in the runnable). This waits until the
     * virification passed or the timeout has been reached. If the timeout has been reached this method will rethrow the
     * {@link MockitoAssertionError} that comes from the mockito verification code.
     * 
     * @param runnable
     *            The runnable containing the mockito verification.
     * @param timeUnit
     *            The timeout timeunit.
     * @param timeout
     *            The timeout.
     * @throws InterruptedException
     */
public static void waitUntilVerified(Runnable runnable, TimeUnit timeUnit, int timeout) throws InterruptedException {
    long startTime = System.currentTimeMillis();
    do {
        MockitoAssertionError exception = null;
        try {
            runnable.run();
        } catch (MockitoAssertionError e) {
            exception = e;
        }
        if (exception == null) {
            return;
        }
        if (System.currentTimeMillis() > startTime + timeUnit.toMillis(timeout)) {
            throw exception;
        }
        Thread.sleep(50);
    } while (true);
}
Also used : MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError)

Example 7 with MockitoAssertionError

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

the class PrivateInstanceMockingTest method errorousVerificationOnPrivateMethodGivesFilteredErrorMessage.

@Test
public void errorousVerificationOnPrivateMethodGivesFilteredErrorMessage() throws Exception {
    PrivateMethodDemo tested = spy(new PrivateMethodDemo());
    assertEquals("Hello Temp, you are 50 old.", tested.sayYear("Temp", 50));
    when(tested, "doSayYear", Mockito.anyInt(), Mockito.anyString()).thenReturn("another");
    assertEquals("another", tested.sayYear("Johan", 29));
    assertEquals("another", tested.sayYear("test", 12));
    try {
        verifyPrivate(tested, never()).invoke("doSayYear", 50, "Temp");
        fail("Should throw assertion error");
    } catch (MockitoAssertionError e) {
        assertEquals("\nsamples.privatemocking.PrivateMethodDemo.doSayYear(\n    50,\n    \"Temp\"\n);\nNever wanted  but invoked .", e.getMessage());
    }
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 8 with MockitoAssertionError

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

the class PrivateInstanceMockingTest method errorousVerificationOnPrivateMethodGivesFilteredErrorMessage.

@Test
public void errorousVerificationOnPrivateMethodGivesFilteredErrorMessage() throws Exception {
    PrivateMethodDemo tested = spy(new PrivateMethodDemo());
    assertEquals("Hello Temp, you are 50 old.", tested.sayYear("Temp", 50));
    when(tested, "doSayYear", Mockito.anyInt(), Mockito.anyString()).thenReturn("another");
    assertEquals("another", tested.sayYear("Johan", 29));
    assertEquals("another", tested.sayYear("test", 12));
    try {
        verifyPrivate(tested, never()).invoke("doSayYear", 50, "Temp");
        fail("Should throw assertion error");
    } catch (MockitoAssertionError e) {
        assertEquals("\nsamples.privatemocking.PrivateMethodDemo.doSayYear(\n    50,\n    \"Temp\"\n);\nNever wanted  but invoked .", e.getMessage());
    }
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 9 with MockitoAssertionError

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

the class PrivateInstanceMockingTest method errorousVerificationOnPrivateMethodGivesFilteredErrorMessage.

@Test
public void errorousVerificationOnPrivateMethodGivesFilteredErrorMessage() throws Exception {
    PrivateMethodDemo tested = spy(new PrivateMethodDemo());
    assertEquals("Hello Temp, you are 50 old.", tested.sayYear("Temp", 50));
    when(tested, "doSayYear", Mockito.anyInt(), Mockito.anyString()).thenReturn("another");
    assertEquals("another", tested.sayYear("Johan", 29));
    assertEquals("another", tested.sayYear("test", 12));
    try {
        verifyPrivate(tested, never()).invoke("doSayYear", 50, "Temp");
        fail("Should throw assertion error");
    } catch (MockitoAssertionError e) {
        assertEquals("\nsamples.privatemocking.PrivateMethodDemo.doSayYear(\n    50,\n    \"Temp\"\n);\nNever wanted  but invoked .", e.getMessage());
    }
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 10 with MockitoAssertionError

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

the class PrivateInstanceMockingTest method errorousVerificationOnPrivateMethodGivesFilteredErrorMessage.

@Test
public void errorousVerificationOnPrivateMethodGivesFilteredErrorMessage() throws Exception {
    PrivateMethodDemo tested = spy(new PrivateMethodDemo());
    assertEquals("Hello Temp, you are 50 old.", tested.sayYear("Temp", 50));
    when(tested, "doSayYear", Mockito.anyInt(), Mockito.anyString()).thenReturn("another");
    assertEquals("another", tested.sayYear("Johan", 29));
    assertEquals("another", tested.sayYear("test", 12));
    try {
        verifyPrivate(tested, never()).invoke("doSayYear", 50, "Temp");
        fail("Should throw assertion error");
    } catch (MockitoAssertionError e) {
        assertEquals("\nsamples.privatemocking.PrivateMethodDemo.doSayYear(\n    50,\n    \"Temp\"\n);\nNever wanted  but invoked .", e.getMessage());
    }
}
Also used : PrivateMethodDemo(samples.privatemocking.PrivateMethodDemo) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) Test(org.junit.Test) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

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