Java 类org.mockito.exceptions.base.MockitoAssertionError 实例源码

项目:powermock    文件:PrivateInstanceMockingTest.java   
@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());
    }
}
项目:powermock    文件:MockStaticTest.java   
@Test
public void errorousVerificationOfStaticMethodsGivesANonMockitoStandardMessage() throws Exception {
    final String expected = "Hello world";
    final String argument = "hello";

    mockStatic(StaticService.class);

    when(StaticService.say(argument)).thenReturn(expected);

    assertEquals(expected, StaticService.say(argument));

    // Verification is done in two steps using static methods.
    verifyStatic(times(2));
    try {
        StaticService.say(argument);
        fail("Should throw assertion error");
    } catch (MockitoAssertionError e) {
        assertEquals("\nsamples.singleton.StaticService.say(\"hello\");\nWanted 2 times but was 1 time.", e.getMessage());
    }
}
项目:powermock    文件:PrivateInstanceMockingTest.java   
@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());
    }
}
项目:powermock    文件:MockStaticTest.java   
@Test
public void errorousVerificationOfStaticMethodsGivesANonMockitoStandardMessage() throws Exception {
    final String expected = "Hello world";
    final String argument = "hello";

    mockStatic(StaticService.class);

    when(StaticService.say(argument)).thenReturn(expected);

    assertEquals(expected, StaticService.say(argument));

    // Verification is done in two steps using static methods.
    verifyStatic(times(2));
    try {
        StaticService.say(argument);
        fail("Should throw assertion error");
    } catch (MockitoAssertionError e) {
        assertEquals("\nsamples.singleton.StaticService.say(\"hello\");\nWanted 2 times but was 1 time.", e.getMessage());
    }
}
项目:powermock    文件:VerifyNoMoreInteractionsTest.java   
@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:"));
    }
}
项目:powermock    文件:PrivateInstanceMockingTest.java   
@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());
    }
}
项目:powermock    文件:MockStaticTest.java   
@Test
public void errorousVerificationOfStaticMethodsGivesANonMockitoStandardMessage() throws Exception {
    final String expected = "Hello world";
    final String argument = "hello";

    mockStatic(StaticService.class);

    when(StaticService.say(argument)).thenReturn(expected);

    assertEquals(expected, StaticService.say(argument));

    // Verification is done in two steps using static methods.
    verifyStatic(times(2));
    try {
        StaticService.say(argument);
        fail("Should throw assertion error");
    } catch (MockitoAssertionError e) {
        assertEquals("\nsamples.singleton.StaticService.say(\"hello\");\nWanted 2 times but was 1 time.", e.getMessage());
    }
}
项目:powermock    文件:PrivateInstanceMockingTest.java   
@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());
    }
}
项目:powermock    文件:MockStaticTest.java   
@Test
public void errorousVerificationOfStaticMethodsGivesANonMockitoStandardMessage() throws Exception {
    final String expected = "Hello world";
    final String argument = "hello";

    mockStatic(StaticService.class);

    when(StaticService.say(argument)).thenReturn(expected);

    assertEquals(expected, StaticService.say(argument));

    // Verification is done in two steps using static methods.
    verifyStatic(times(2));
    try {
        StaticService.say(argument);
        fail("Should throw assertion error");
    } catch (MockitoAssertionError e) {
        assertEquals("\nsamples.singleton.StaticService.say(\"hello\");\nWanted 2 times but was 1 time.", e.getMessage());
    }
}
项目:powermock    文件:MockitoNewInvocationControl.java   
public Object invoke(Class<?> type, Object[] args, Class<?>[] sig) throws Exception {
    Constructor<?> constructor = WhiteboxImpl.getConstructor(type, sig);
    if (constructor.isVarArgs()) {
        Object varArgs =  args[args.length - 1];
           final int varArgsLength = Array.getLength(varArgs);
           Object[] oldArgs = args;
           args = new Object[args.length + varArgsLength - 1];
           System.arraycopy(oldArgs, 0, args, 0, oldArgs.length - 1);
           for (int i = oldArgs.length - 1, j=0; i < args.length; i++, j++) {
               args[i] = Array.get(varArgs, j);                                     
           }
    }
    try {
        return substitute.performSubstitutionLogic(args);
    } catch (MockitoAssertionError e) {
        InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type);
    }

    // Won't happen
    return null;
}
项目:AndroidMvc    文件:LifeCycleValidator.java   
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;
    }
}
项目:astor    文件:VerificationWithTimeoutImpl.java   
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;
    }
}
项目:astor    文件:VerificationAfterDelayTest.java   
@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);
}
项目:camunda-bpm-platform    文件:NoIntermediaryInvocation.java   
@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());
  }
}
项目:camunda-bpm-platform    文件:NoIntermediaryInvocationTest.java   
@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
  }
}
项目:camunda-bpm-platform    文件:NoIntermediaryInvocationTest.java   
@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
  }
}
项目:camunda-bpm-platform    文件:NoIntermediaryInvocationTest.java   
@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
  }
}
项目:camunda-bpm-platform    文件:NoIntermediaryInvocationTest.java   
@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
  }
}
项目:GitHub    文件:RequestManagerFragmentTest.java   
private void runTest(TestCase testCase) {
  for (Harness harness : harnesses) {
    try {
      testCase.runTest(harness);
    } catch (MockitoAssertionError e) {
      throw new Error("Failed to get expected call on " + harness, e);
    }
  }
}
项目:GitHub    文件:RequestManagerFragmentTest.java   
private void runTest(TestCase testCase) {
  for (Harness harness : harnesses) {
    try {
      testCase.runTest(harness);
    } catch (MockitoAssertionError e) {
      throw new Error("Failed to get expected call on " + harness, e);
    }
  }
}
项目:powermock    文件:VerifyNoMoreInteractionsTest.java   
@Test
public void verifyNoMoreInteractionsOnMethodThrowsAssertionErrorWhenMoreInteractionsTookPlace() throws Exception {
    mockStatic(StaticService.class);
    assertNull(StaticService.say("hello"));

    try {
        verifyNoMoreInteractions(StaticService.class);
        fail("Should throw exception!");
    } catch (MockitoAssertionError e) {
        assertTrue(e
                .getMessage()
                .startsWith(
                        "\nNo interactions wanted here:\n-> at samples.powermockito.junit4.verifynomoreinteractions.VerifyNoMoreInteractionsTest.verifyNoMoreInteractionsOnMethodThrowsAssertionErrorWhenMoreInteractionsTookPlace(VerifyNoMoreInteractionsTest.java"));
    }
}
项目:powermock    文件:MockitoNewInvocationControl.java   
public void verifyNoMoreInteractions() {
    try {
        Mockito.verifyNoMoreInteractions(substitute);
    } catch (MockitoAssertionError e) {
        InvocationControlAssertionError.updateErrorMessageForVerifyNoMoreInteractions(e);
        throw e;
    }
}
项目:powermock    文件:DefaultConstructorArgumentsVerfication.java   
private void invokeSubstitute(Object... arguments) throws Exception {
    try {
        invocationControl.getSubstitute().performSubstitutionLogic(arguments);
    } catch (MockitoAssertionError e) {
        InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type);
    }
}
项目:astor    文件:VerificationWithTimeoutTest.java   
@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) {}
}
项目:astor    文件:AtMostXVerificationTest.java   
@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) {}
}
项目:astor    文件:AtMostXVerificationTest.java   
@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) {}
}
项目:astor    文件:AtMostXVerificationTest.java   
@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());
    }
}
项目:astor    文件:AtLeastXVerificationTest.java   
@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) {}
}
项目:astor    文件:OnlyTest.java   
@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());
}
项目:astor    文件:TimeoutTest.java   
@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) {}
}
项目:astor    文件:TimeoutTest.java   
@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);
}
项目:astor    文件:VerificationOverTimeImpl.java   
/**
 * 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;
    }
}
项目:astor    文件:VerificationWithTimeoutTest.java   
@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) {}
}
项目:astor    文件:AtMostXVerificationTest.java   
@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) {}
}
项目:astor    文件:AtMostXVerificationTest.java   
@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) {}
}
项目:astor    文件:AtMostXVerificationTest.java   
@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());
    }
}
项目:astor    文件:AtLeastXVerificationTest.java   
@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) {}
}
项目:astor    文件:OnlyTest.java   
@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());
}
项目:astor    文件:TimeoutTest.java   
@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) {}
}
项目:sejda    文件:OnceWithMessage.java   
@Override
public void verify(VerificationData data) {
    try {
        if (wantedCount > 0) {
            MissingInvocationChecker missingInvocation = new MissingInvocationChecker();
            missingInvocation.check(data.getAllInvocations(), data.getWanted());
        }
        NumberOfInvocationsChecker numberOfInvocations = new NumberOfInvocationsChecker();
        numberOfInvocations.check(data.getAllInvocations(), data.getWanted(), wantedCount);
    } catch (MockitoAssertionError mae) {
        throw new SejdaRuntimeException(failureDescribingMessage, mae);
    }
}