@Test public void should_fail_at_updating_tax_factor_and_transfer_tax_in_specified_order_due_to_greedy_at_least() throws Exception { // given Person person = new Person(); // when systemUnderTest.transferTaxFor(person); // then InOrder inOrder = Mockito.inOrder(taxService); inOrder.verify(taxService).updateTaxFactor(eq(person), anyDouble()); inOrder.verify(taxService, atLeastOnce()).transferTaxFor(person); try { inOrder.verify(taxService).updateTaxFactor(eq(person), anyDouble()); inOrder.verify(taxService).transferTaxFor(person); } catch (VerificationInOrderFailure verificationInOrderFailure) {} }
/** Expected added only for the test to pass **/ @Test(expectedExceptions = VerificationInOrderFailure.class) public void should_fail_at_updating_second_tax_factor_in_specified_order_due_to_greedy_at_least() throws Exception { // given Person person = new Person(); // when systemUnderTest.transferTaxFor(person); // then InOrder inOrder = Mockito.inOrder(taxService); inOrder.verify(taxService).updateTaxFactor(eq(person), anyDouble()); inOrder.verify(taxService, atLeastOnce()).transferTaxFor(person); inOrder.verify(taxService).updateTaxFactor(eq(person), anyDouble()); inOrder.verify(taxService).transferTaxFor(person); }
/** Expected added only for the test to pass **/ @Test(expected = VerificationInOrderFailure.class) public void should_fail_at_updating_second_tax_factor_in_specified_order_due_to_greedy_at_least() throws Exception { // given Person person = new Person(); // when systemUnderTest.transferTaxFor(person); // then InOrder inOrder = Mockito.inOrder(taxService); inOrder.verify(taxService).updateTaxFactor(eq(person), anyDouble()); inOrder.verify(taxService, atLeastOnce()).transferTaxFor(person); inOrder.verify(taxService).updateTaxFactor(eq(person), anyDouble()); inOrder.verify(taxService).transferTaxFor(person); }
@Test public void shouldPrintMethodThatWasNotInvoked() { inOrder.verify(one).simpleMethod(1); inOrder.verify(one).simpleMethod(11); inOrder.verify(two, times(2)).simpleMethod(2); inOrder.verify(three).simpleMethod(3); try { inOrder.verify(three).simpleMethod(999); fail(); } catch (VerificationInOrderFailure e) { String actualMessage = e.getMessage(); String expectedMessage = "\n" + "Verification in order failure" + "\n" + "Wanted but not invoked:" + "\n" + "iMethods.simpleMethod(999);"; assertContains(expectedMessage, actualMessage); } }
@Test public void shouldVerifySingleMockInOrderAndNotInOrder() { mockOne = mock(IMethods.class); inOrder = inOrder(mockOne); mockOne.simpleMethod(1); mockOne.simpleMethod(2); verify(mockOne).simpleMethod(2); verify(mockOne).simpleMethod(1); inOrder.verify(mockOne).simpleMethod(2); try { inOrder.verify(mockOne).simpleMethod(1); fail(); } catch (VerificationInOrderFailure e) {} }
@Test public void shouldVerifyInOrderWhenTwoChunksAreEqual() { mockOne.simpleMethod(); mockOne.simpleMethod(); mockTwo.differentMethod(); mockOne.simpleMethod(); mockOne.simpleMethod(); inOrder.verify(mockOne, times(2)).simpleMethod(); inOrder.verify(mockTwo).differentMethod(); inOrder.verify(mockOne, times(2)).simpleMethod(); try { inOrder.verify(mockOne, atLeastOnce()).simpleMethod(); fail(); } catch (VerificationInOrderFailure e) {} }
@Test public void shouldVerifyInOrderUsingMatcher() { mockOne.simpleMethod(1); mockOne.simpleMethod(2); mockTwo.differentMethod(); mockOne.simpleMethod(3); mockOne.simpleMethod(4); verify(mockOne, times(4)).simpleMethod(anyInt()); inOrder.verify(mockOne, times(2)).simpleMethod(anyInt()); inOrder.verify(mockTwo).differentMethod(); inOrder.verify(mockOne, times(2)).simpleMethod(anyInt()); try { inOrder.verify(mockOne, times(3)).simpleMethod(anyInt()); fail(); } catch (VerificationInOrderFailure e) {} }
@Test(expected = VerificationInOrderFailure.class) public void inOrderFailure() { whenCalling(this::voidThing); thenVerify(dependency).someMethod("thing"); andVerify(dependency).anotherMethod("bing"); andVerify(anotherDependency).boringMethod(); }
public void wantedButNotInvokedInOrder(PrintableInvocation wanted, PrintableInvocation previous) { throw new VerificationInOrderFailure(join( "Verification in order failure", "Wanted but not invoked:", wanted.toString(), new Location(), "Wanted anywhere AFTER following interaction:", previous.toString(), previous.getLocation(), "" )); }
public void tooLittleActualInvocationsInOrder(Discrepancy discrepancy, PrintableInvocation wanted, Location lastActualLocation) { String message = createTooLittleInvocationsMessage(discrepancy, wanted, lastActualLocation); throw new VerificationInOrderFailure(join( "Verification in order failure:" + message )); }
@Test public void shouldFailOnWrongMethodAfterLastInvocation() { inOrder.verify(mockOne).simpleMethod(4); try { inOrder.verify(mockOne).simpleMethod(999); fail(); } catch (VerificationInOrderFailure e) {} }
@Test public void shouldFilterStacktraceWhenVerifyingInOrder() { InOrder inOrder = inOrder(mock); mock.oneArg(true); mock.oneArg(false); inOrder.verify(mock).oneArg(false); try { inOrder.verify(mock).oneArg(true); fail(); } catch (VerificationInOrderFailure e) { assertThat(e, hasFirstMethodInStackTrace("shouldFilterStacktraceWhenVerifyingInOrder")); } }
@Test public void shouldPointStackTraceToPreviousVerified() { inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt()); inOrder.verify(mockTwo).simpleMethod(anyInt()); try { inOrder.verify(mock).simpleMethod(999); fail(); } catch (VerificationInOrderFailure e) { assertContains("fourth(", e.getMessage()); } }
@Test public void shouldPointToThirdMethod() { inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt()); try { inOrder.verify(mockTwo).simpleMethod(999); fail(); } catch (VerificationInOrderFailure e) { assertContains("third(", e.getMessage()); } }
@Test public void shouldPointToFirstMethodBecauseOfTooManyActualInvocations() { try { inOrder.verify(mock, times(0)).simpleMethod(anyInt()); fail(); } catch (VerificationInOrderFailure e) { assertContains("first(", e.getMessage()); } }
@Test public void shouldPointToSecondMethodBecauseOfTooManyActualInvocations() { inOrder.verify(mock).simpleMethod(anyInt()); try { inOrder.verify(mockTwo, times(0)).simpleMethod(anyInt()); fail(); } catch (VerificationInOrderFailure e) { assertContains("second(", e.getMessage()); } }
@Test public void shouldPointToFourthMethodBecauseOfTooLittleActualInvocations() { inOrder.verify(mock).simpleMethod(anyInt()); inOrder.verify(mockTwo).simpleMethod(anyInt()); inOrder.verify(mock).simpleMethod(anyInt()); try { inOrder.verify(mockTwo, times(3)).simpleMethod(anyInt()); fail(); } catch (VerificationInOrderFailure e) { assertContains("fourth(", e.getMessage()); } }
@Test public void shouldPointStackTraceToPreviousInvocation() { inOrder.verify(mock, times(2)).simpleMethod(anyInt()); inOrder.verify(mockTwo, times(2)).simpleMethod(anyInt()); try { inOrder.verify(mock).simpleMethod(999); fail(); } catch (VerificationInOrderFailure e) { assertContains("secondChunk(", e.getMessage()); } }
@Test public void shouldPointToThirdChunkWhenTooLittleActualInvocations() { inOrder.verify(mock, times(2)).simpleMethod(anyInt()); inOrder.verify(mockTwo, times(2)).simpleMethod(anyInt()); inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt()); try { inOrder.verify(mockTwo, times(3)).simpleMethod(999); fail(); } catch (VerificationInOrderFailure e) { assertContains("thirdChunk(", e.getMessage()); } }
@Test public void shouldPointToFourthChunkBecauseTooManyActualInvocations() { inOrder.verify(mock, atLeastOnce()).simpleMethod(anyInt()); try { inOrder.verify(mockTwo, times(0)).simpleMethod(anyInt()); fail(); } catch (VerificationInOrderFailure e) { assertContains("fourthChunk(", e.getMessage()); } }
@Test public void shouldSayNoInteractionsWanted() throws Exception { //when mock.simpleMethod(); //then InOrder inOrder = inOrder(mock); try { inOrder.verifyNoMoreInteractions(); fail(); } catch(VerificationInOrderFailure e) { assertContains("No interactions wanted", e.getMessage()); } }
@Test public void shouldFailToVerifyNoMoreInteractionsInOrder() throws Exception { //when mock.simpleMethod(); mock.simpleMethod(10); mock.otherMethod(); //then InOrder inOrder = inOrder(mock); inOrder.verify(mock).simpleMethod(10); try { inOrder.verifyNoMoreInteractions(); fail(); } catch(VerificationInOrderFailure e) {} }
@Test public void shouldFailToVerifyNoMoreInteractionsInOrderWithMultipleMocks() throws Exception { //when mock.simpleMethod(); mock2.simpleMethod(); mock.otherMethod(); //then InOrder inOrder = inOrder(mock, mock2); inOrder.verify(mock2).simpleMethod(); try { inOrder.verifyNoMoreInteractions(); fail(); } catch(VerificationInOrderFailure e) {} }
@Test public void shouldFailOnSecondMethodBecauseFourInvocationsWanted() { inOrder.verify(mockOne, times(1)).simpleMethod(1); try { inOrder.verify(mockTwo, times(4)).simpleMethod(2); fail(); } catch (VerificationInOrderFailure e) { } }
@Test public void shouldFailOnLastInvocationTooEarly() { inOrder.verify(mockThree).simpleMethod(4); verify(mockThree).simpleMethod(4); verify(mockTwo).simpleMethod(2); try { inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); fail(); } catch (VerificationInOrderFailure e) {} }
@Test public void shouldFailOnLastMethodBecauseOneInvocationWanted() { inOrder.verify(mockOne, atLeastOnce()).simpleMethod(1); inOrder.verify(mockTwo, times(2)).simpleMethod(2); inOrder.verify(mockThree, atLeastOnce()).simpleMethod(3); inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2); try { inOrder.verify(mockOne, times(0)).simpleMethod(4); fail(); } catch (VerificationInOrderFailure e) { } }
@Test public void shouldNotSayArgumentsAreDifferent() { //this is the last invocation so any next verification in order should simply say wanted but not invoked inOrder.verify(three).simpleMethod(3); try { inOrder.verify(one).simpleMethod(999); fail(); } catch (VerificationInOrderFailure e) { assertContains("Wanted but not invoked", e.getMessage()); } }
@Test public void shouldPrintTooManyInvocations() { inOrder.verify(one).simpleMethod(1); inOrder.verify(one).simpleMethod(11); try { inOrder.verify(two, times(1)).simpleMethod(2); fail(); } catch (VerificationInOrderFailure e) { String actualMessage = e.getMessage(); String expectedMessage = "\n" + "Verification in order failure:" + "\n" + "iMethods.simpleMethod(2);" + "\n" + "Wanted 1 time:" + "\n" + "-> at"; assertContains(expectedMessage, actualMessage); String expectedCause = "\n" + "But was 2 times. Undesired invocation:" + "\n" + "-> at"; assertContains(expectedCause, e.getMessage()); } }
@Test public void shouldReportTooManyInvocations() throws Exception { Invocation first = new InvocationBuilder().toInvocation(); Invocation second = new InvocationBuilder().toInvocation(); finderStub.validMatchingChunkToReturn.addAll(asList(first, second)); try { checker.check(invocations, wanted, 1, context); fail(); } catch (VerificationInOrderFailure e) { assertContains("Wanted 1 time", e.getMessage()); assertContains("But was 2 times", e.getMessage()); } }
@Test public void shouldFailVerificationForMockOne() { InOrder inOrder = inOrder(mockOne); inOrder.verify(mockOne).simpleMethod(1); try { inOrder.verify(mockOne).differentMethod(); fail(); } catch (VerificationInOrderFailure e) {} }
@Test public void shouldFailVerificationForMockOneBecauseOfWrongOrder() { InOrder inOrder = inOrder(mockOne); inOrder.verify(mockOne).simpleMethod(4); try { inOrder.verify(mockOne).simpleMethod(1); fail(); } catch (VerificationInOrderFailure e) {} }
@Test public void shouldFailVerificationForMockTwo() { InOrder inOrder = inOrder(mockTwo); try { inOrder.verify(mockTwo).simpleMethod(2); fail(); } catch (VerificationInOrderFailure e) {} }
@Test public void shouldThrowNoMoreInvocationsForMockTwo() { InOrder inOrder = inOrder(mockTwo); try { inOrder.verify(mockTwo, times(2)).simpleMethod(2); fail(); } catch (VerificationInOrderFailure e) {} }
@Test public void shouldThrowTooLittleInvocationsForMockTwo() { InOrder inOrder = inOrder(mockTwo); try { inOrder.verify(mockTwo, times(4)).simpleMethod(2); fail(); } catch (VerificationInOrderFailure e) {} }
@Test public void shouldThrowTooManyInvocationsForMockTwo() { InOrder inOrder = inOrder(mockTwo); try { inOrder.verify(mockTwo, times(2)).simpleMethod(2); fail(); } catch (VerificationInOrderFailure e) {} }
@Test public void shouldFailWhenMiddleMethodVerifiedFirst() { inOrder.verify(mockTwo, times(2)).simpleMethod(2); try { inOrder.verify(mockOne).simpleMethod(1); fail(); } catch (VerificationInOrderFailure e) { } }
@Test public void shouldFailTimesZeroInOrder() { inOrder.verify(mockTwo, times(2)).simpleMethod(2); try { inOrder.verify(mockThree, times(0)).simpleMethod(3); fail(); } catch (VerificationInOrderFailure e) {} }