@Test public void shouldVerifyActualNumberOfInvocationsSmallerThanWanted() throws Exception { mock.clear(); mock.clear(); mock.clear(); Mockito.verify(mock, times(3)).clear(); try { Mockito.verify(mock, times(100)).clear(); fail(); } catch (TooLittleActualInvocations e) { assertContains("mock.clear();", e.getMessage()); assertContains("Wanted 100 times", e.getMessage()); assertContains("was 3", e.getMessage()); } }
@Test(expected = TooLittleActualInvocations.class) public void testMockStaticIncorrectTimes() throws Exception { mockStatic(StaticService.class); assertNull(StaticService.say("hello")); assertNull(StaticService.say("hello")); // Verification is done in two steps using static methods. verifyStatic(times(3)); StaticService.say("hello"); }
@Test public void shouldDetectTooLittleActualInvocations() throws Exception { mock.clear(); mock.clear(); verify(mock, times(2)).clear(); try { verify(mock, times(100)).clear(); fail(); } catch (TooLittleActualInvocations e) { assertContains("Wanted 100 times", e.getMessage()); assertContains("was 2", e.getMessage()); } }
@Test public void shouldVerifyNumberOfTimesAndFail() { spy.add("one"); spy.add("one"); try { verify(spy, times(3)).add("one"); fail(); } catch (TooLittleActualInvocations e) {} }
public void tooLittleActualInvocations(Discrepancy discrepancy, PrintableInvocation wanted, Location lastActualLocation) { String message = createTooLittleInvocationsMessage(discrepancy, wanted, lastActualLocation); throw new TooLittleActualInvocations(message); }
@Test(expected=TooLittleActualInvocations.class) public void shouldLetPassingNullLastActualStackTrace() throws Exception { new Reporter().tooLittleActualInvocations(new Discrepancy(1, 2), new InvocationBuilder().toInvocation(), null); }
public void tooLittleActualInvocations(org.mockito.internal.reporting.Discrepancy discrepancy, DescribedInvocation wanted, Location lastActualLocation) { String message = createTooLittleInvocationsMessage(discrepancy, wanted, lastActualLocation); throw new TooLittleActualInvocations(message); }
@Test(expected=TooLittleActualInvocations.class) public void shouldLetPassingNullLastActualStackTrace() throws Exception { new Reporter().tooLittleActualInvocations(new org.mockito.internal.reporting.Discrepancy(1, 2), new InvocationBuilder().toInvocation(), null); }