/* * Copyright (c) 2007 Mockito contributors * This program is made available under the terms of the MIT License. */ package org.mockitousage.verification; import org.junit.Before; import org.junit.Test; import org.mockito.InOrder; import org.mockito.exceptions.verification.VerificationInOrderFailure; import org.mockito.exceptions.verification.WantedButNotInvoked; import org.mockitousage.IMethods; import org.mockitoutil.TestBase; import static junit.framework.TestCase.fail; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.*; public class VerificationInOrderTest extends TestBase { private IMethods mockOne; private IMethods mockTwo; private IMethods mockThree; private InOrder inOrder; @Before public void setUp() { mockOne = mock(IMethods.class); mockTwo = mock(IMethods.class); mockThree = mock(IMethods.class); inOrder = inOrder(mockOne, mockTwo, mockThree); } @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 shouldMessagesPointToProperMethod() { mockTwo.differentMethod(); mockOne.simpleMethod(); try { inOrder.verify(mockOne, atLeastOnce()).differentMethod(); fail(); } catch (WantedButNotInvoked e) { assertThat(e).hasMessageContaining("differentMethod()"); } } @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) {} } }