org.mockito.exceptions.base.MockitoAssertionError Java Examples

The following examples show how to use org.mockito.exceptions.base.MockitoAssertionError. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: NoIntermediaryInvocationTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureWithIntermittentInvocations() {
  // given
  foo.getFoo();
  foo.getBaz();
  foo.getBar();

  // when
  InOrder inOrder = Mockito.inOrder(foo);
  inOrder.verify(foo).getFoo();

  // then
  try {
    inOrder.verify(foo, immediatelyAfter()).getBar();
    Assert.fail("should not verify");
  } catch (MockitoAssertionError e) {
    // happy path
  }
}
 
Example #2
Source File: VerificationWithTimeoutImpl.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void verify(VerificationData data) {
    int soFar = 0;
    MockitoAssertionError error = null;
    while (soFar <= timeout) {
        try {
            delegate.verify(data);
            return;
        } catch (MockitoAssertionError e) {
            error = e;
            soFar += treshhold;
            sleep(treshhold);
        }
    }
    if (error != null) {
        throw error;
    }
}
 
Example #3
Source File: NoIntermediaryInvocationTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureOnWrongInvocationOrder() {
  // given
  foo.getBar();
  foo.getFoo();

  // when
  InOrder inOrder = Mockito.inOrder(foo);
  inOrder.verify(foo).getFoo();

  // then
  try {
    inOrder.verify(foo, immediatelyAfter()).getBar();
    Assert.fail("should not verify");
  } catch (MockitoAssertionError e) {
    // happy path
  }
}
 
Example #4
Source File: NoIntermediaryInvocationTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureWhenInvocationNotPresentCase2() {
  // given
  foo.getFoo();

  // when
  InOrder inOrder = Mockito.inOrder(foo);
  inOrder.verify(foo).getFoo();

  // then
  try {
    inOrder.verify(foo, immediatelyAfter()).getBar();
    Assert.fail("should not verify");
  } catch (MockitoAssertionError e) {
    // happy path
  }
}
 
Example #5
Source File: NoIntermediaryInvocationTest.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Test
public void testFailureWhenInvocationNotPresent() {
  // given
  foo.getFoo();
  foo.getBaz();

  // when
  InOrder inOrder = Mockito.inOrder(foo);
  inOrder.verify(foo).getFoo();

  // then
  try {
    inOrder.verify(foo, immediatelyAfter()).getBar();
    Assert.fail("should not verify");
  } catch (MockitoAssertionError e) {
    // happy path
  }
}
 
Example #6
Source File: NoIntermediaryInvocation.java    From camunda-bpm-platform with Apache License 2.0 6 votes vote down vote up
@Override
public void verifyInOrder(VerificationDataInOrder data) {

  Invocation firstUnverifiedInvocation = finder.findFirstUnverifiedInOrder(data.getOrderingContext(), data.getAllInvocations());

  if (firstUnverifiedInvocation == null) {
    Invocation previouslyVerified = finder.findPreviousVerifiedInOrder(data.getAllInvocations(), data.getOrderingContext());
    new Reporter().wantedButNotInvokedInOrder(data.getWanted(), previouslyVerified);
  }

  if (!data.getWanted().matches(firstUnverifiedInvocation)) {
    StringBuilder sb = new StringBuilder();
    sb.append("Expected next invocation specified here: \n");
    sb.append(data.getWanted().getLocation());
    sb.append("\n");
    sb.append("but next invocation was: \n");
    sb.append(firstUnverifiedInvocation.getLocation());
    sb.append("\n");

    throw new MockitoAssertionError(sb.toString());
  }
}
 
Example #7
Source File: LazyVerificationUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void whenLazilyVerified_thenReportsMultipleFailures() {
    VerificationCollector collector = MockitoJUnit.collector()
        .assertLazily();

    List mockList = mock(List.class);
    verify(mockList).add("one");
    verify(mockList).clear();

    try {
        collector.collectAndReport();
    } catch (MockitoAssertionError error) {
        assertTrue(error.getMessage()
            .contains("1. Wanted but not invoked:"));
        assertTrue(error.getMessage()
            .contains("2. Wanted but not invoked:"));
    }
}
 
Example #8
Source File: VerificationAfterDelayTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void shouldWaitTheFullTimeIfTheTestCouldPass() throws Exception {
    // given
    Thread t = waitAndExerciseMock(50);

    // when
    t.start();

    // then        
    long startTime = System.currentTimeMillis();
    
    try {
        verify(mock, after(100).atLeast(2)).clear();
        fail();
    } catch (MockitoAssertionError e) {}
    
    assertTrue(System.currentTimeMillis() - startTime >= 100);
}
 
Example #9
Source File: LifeCycleValidator.java    From AndroidMvc with Apache License 2.0 6 votes vote down vote up
public void expect(LifeCycle... lifeCycles){
    long start = System.currentTimeMillis();
    while(true) {
        long elapse = System.currentTimeMillis() - start;
        try {
            doExpect(lifeCycles);
        } catch (MockitoAssertionError mockitoAssertionError) {
            if (elapse > wait_span) {
                throw mockitoAssertionError;
            } else {
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

            }
        }

        //Pass
        break;
    }
}
 
Example #10
Source File: VerificationWithTimeoutTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailVerificationWithTimeout() throws Exception {
    //given
    Thread t = waitAndExerciseMock(80);
    
    //when
    t.start();
    
    //then
    verify(mock, never()).clear();
    try {
        verify(mock, timeout(20).atLeastOnce()).clear();
        fail();
    } catch (MockitoAssertionError e) {}
}
 
Example #11
Source File: TimeoutTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void should_fail_because_verification_fails() {
    Timeout t = new Timeout(1, 2, mode);
    
    doThrow(error).
    doThrow(error).
    doThrow(error).
    when(mode).verify(data);
    
    try {
        t.verify(data);
        fail();
    } catch (MockitoAssertionError e) {}
}
 
Example #12
Source File: OnlyTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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());
}
 
Example #13
Source File: AtLeastXVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailVerifiationAtLeastXTimes() throws Exception {
    mock.add("one");
    verify(mock, atLeast(1)).add(anyString());

    try {
        verify(mock, atLeast(2)).add(anyString());
        fail();
    } catch (MockitoAssertionError e) {}
}
 
Example #14
Source File: AtMostXVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldPrintDecentMessage() throws Exception {
    mock.clear();
    mock.clear();
    
    try {
        verify(mock, atMost(1)).clear();
        fail();
    } catch (MockitoAssertionError e) {
        assertEquals("\nWanted at most 1 time but was 2", e.getMessage());
    }
}
 
Example #15
Source File: AtMostXVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldWorkWithArgumentMatchers() throws Exception {
    mock.add("one");
    verify(mock, atMost(5)).add(anyString());
    
    try {
        verify(mock, atMost(0)).add(anyString());
        fail();
    } catch (MockitoAssertionError e) {}
}
 
Example #16
Source File: AtMostXVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldVerifyAtMostXTimes() throws Exception {
    mock.clear();
    mock.clear();
    
    verify(mock, atMost(2)).clear();
    verify(mock, atMost(3)).clear();
    
    try {
        verify(mock, atMost(1)).clear();
        fail();
    } catch (MockitoAssertionError e) {}
}
 
Example #17
Source File: VerificationOverTimeImpl.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 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) {
    MockitoAssertionError error = null;
    
    long startTime = System.currentTimeMillis();
    while (System.currentTimeMillis() - startTime <= durationMillis) {
        try {
            delegate.verify(data);
            
            if (returnOnSuccess) {
                return;
            } else {
                error = null;
            }
        } catch (MockitoAssertionError e) {
            if (canRecoverFromFailure(delegate)) {
                error = e;
                sleep(pollingPeriodMillis);
            } else {
                throw e;
            }
        }
    }
    
    if (error != null) {
        throw error;
    }
}
 
Example #18
Source File: TimeoutTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldTryToVerifyCorrectNumberOfTimes() {
    Timeout t = new Timeout(1, 4, mode);
    
    doThrow(error).when(mode).verify(data);
    
    try {
        t.verify(data);
        fail();
    } catch (MockitoAssertionError e) {};
    
    verify(mode, times(5)).verify(data);
}
 
Example #19
Source File: TimeoutTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailBecauseVerificationFails() {
    Timeout t = new Timeout(1, 2, mode);
    
    doThrow(error).
    doThrow(error).
    doThrow(error).        
    when(mode).verify(data);
    
    try {
        t.verify(data);
        fail();
    } catch (MockitoAssertionError e) {}
}
 
Example #20
Source File: OnlyTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@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());
}
 
Example #21
Source File: AtLeastXVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailVerifiationAtLeastXTimes() throws Exception {
    mock.add("one");
    verify(mock, atLeast(1)).add(anyString());

    try {
        verify(mock, atLeast(2)).add(anyString());
        fail();
    } catch (MockitoAssertionError e) {}
}
 
Example #22
Source File: AtMostXVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldPrintDecentMessage() throws Exception {
    mock.clear();
    mock.clear();
    
    try {
        verify(mock, atMost(1)).clear();
        fail();
    } catch (MockitoAssertionError e) {
        assertEquals("\nWanted at most 1 time but was 2", e.getMessage());
    }
}
 
Example #23
Source File: AtMostXVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldWorkWithArgumentMatchers() throws Exception {
    mock.add("one");
    verify(mock, atMost(5)).add(anyString());
    
    try {
        verify(mock, atMost(0)).add(anyString());
        fail();
    } catch (MockitoAssertionError e) {}
}
 
Example #24
Source File: AtMostXVerificationTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldVerifyAtMostXTimes() throws Exception {
    mock.clear();
    mock.clear();
    
    verify(mock, atMost(2)).clear();
    verify(mock, atMost(3)).clear();
    
    try {
        verify(mock, atMost(1)).clear();
        fail();
    } catch (MockitoAssertionError e) {}
}
 
Example #25
Source File: VerificationWithTimeoutTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void shouldFailVerificationWithTimeout() throws Exception {
    //given
    Thread t = waitAndExerciseMock(40);
    
    //when
    t.start();
    
    //then
    verify(mock, never()).clear();
    try {
        verify(mock, timeout(20).atLeastOnce()).clear();
        fail();
    } catch (MockitoAssertionError e) {}
}
 
Example #26
Source File: Reporter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void wantedAtMostX(int maxNumberOfInvocations, int foundSize) {
    throw new MockitoAssertionError(join("Wanted at most " + pluralize(maxNumberOfInvocations) + " but was " + foundSize));
}
 
Example #27
Source File: Reporter.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
public void wantedAtMostX(int maxNumberOfInvocations, int foundSize) {
    throw new MockitoAssertionError(join("Wanted at most " + pluralize(maxNumberOfInvocations) + " but was " + foundSize));
}