/*
* 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.NeverWantedButInvoked;
import org.mockito.exceptions.verification.NoInteractionsWanted;
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.mockito.Mockito.*;
/**
* ignored since 'relaxed' in order verification is not implemented (too complex to bother, maybe later).
*/
public class RelaxedVerificationInOrderTest 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);
mockOne.simpleMethod(1);
mockTwo.simpleMethod(2);
mockTwo.simpleMethod(2);
mockThree.simpleMethod(3);
mockTwo.simpleMethod(2);
mockOne.simpleMethod(4);
}
@Test
public void shouldVerifyInOrderAllInvocations() {
inOrder.verify(mockOne).simpleMethod(1);
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
inOrder.verify(mockThree).simpleMethod(3);
inOrder.verify(mockTwo).simpleMethod(2);
inOrder.verify(mockOne).simpleMethod(4);
verifyNoMoreInteractions(mockOne, mockTwo, mockThree);
}
@Test
public void shouldVerifyInOrderAndBeRelaxed() {
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
inOrder.verify(mockThree).simpleMethod(3);
verifyNoMoreInteractions(mockThree);
}
@Test
public void shouldAllowFirstChunkBeforeLastInvocation() {
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
inOrder.verify(mockOne).simpleMethod(4);
try {
verifyNoMoreInteractions(mockTwo);
fail();
} catch (NoInteractionsWanted e) {}
}
@Test
public void shouldAllowAllChunksBeforeLastInvocation() {
inOrder.verify(mockTwo, times(3)).simpleMethod(2);
inOrder.verify(mockOne).simpleMethod(4);
verifyNoMoreInteractions(mockTwo);
}
@Test
public void shouldVerifyDetectFirstChunkOfInvocationThatExistInManyChunks() {
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
inOrder.verify(mockThree).simpleMethod(3);
try {
verifyNoMoreInteractions(mockTwo);
fail();
} catch(NoInteractionsWanted e) {}
}
@Test
public void shouldVerifyDetectAllChunksOfInvocationThatExistInManyChunks() {
inOrder.verify(mockTwo, times(3)).simpleMethod(2);
inOrder.verify(mockOne).simpleMethod(4);
verifyNoMoreInteractions(mockTwo);
}
@Test
public void shouldVerifyInteractionsFromAllChunksWhenAtLeastOnceMode() {
inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
verifyNoMoreInteractions(mockTwo);
try {
inOrder.verify(mockThree).simpleMethod(3);
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldVerifyInteractionsFromFirstChunk() {
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
try {
verifyNoMoreInteractions(mockTwo);
fail();
} catch (NoInteractionsWanted e) {}
}
@Test(expected=VerificationInOrderFailure.class)
public void shouldFailVerificationOfNonFirstChunk() {
inOrder.verify(mockTwo, times(1)).simpleMethod(2);
}
@Test
public void shouldPassOnCombinationOfTimesAndAtLeastOnce() {
mockTwo.simpleMethod(2);
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
verifyNoMoreInteractions(mockTwo);
}
@Test
public void shouldPassOnEdgyCombinationOfTimesAndAtLeastOnce() {
mockTwo.simpleMethod(2);
mockThree.simpleMethod(3);
inOrder.verify(mockThree).simpleMethod(3);
inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
inOrder.verify(mockThree).simpleMethod(3);
verifyNoMoreInteractions(mockThree);
}
@Test
public void shouldVerifyInOrderMockTwoAndThree() {
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
inOrder.verify(mockThree).simpleMethod(3);
inOrder.verify(mockTwo).simpleMethod(2);
verifyNoMoreInteractions(mockTwo, mockThree);
}
@Test
public void shouldVerifyInOrderMockOneAndThree() {
inOrder.verify(mockOne).simpleMethod(1);
inOrder.verify(mockThree).simpleMethod(3);
inOrder.verify(mockOne).simpleMethod(4);
verifyNoMoreInteractions(mockOne, mockThree);
}
@Test
public void shouldVerifyInOrderOnlyTwoInvocations() {
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
inOrder.verify(mockOne).simpleMethod(4);
}
@Test
public void shouldVerifyInOrderOnlyMockTwo() {
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
inOrder.verify(mockTwo).simpleMethod(2);
verifyNoMoreInteractions(mockTwo);
}
@Test
public void shouldVerifyMockTwoCalledTwice() {
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
}
@Test
public void shouldVerifyMockTwoCalledAtLeastOnce() {
inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
}
@Test(expected=WantedButNotInvoked.class)
public void shouldFailOnWrongMethodCalledOnMockTwo() {
inOrder.verify(mockTwo, atLeastOnce()).differentMethod();
}
@Test
public void shouldAllowTimesZeroButOnlyInOrder() {
inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
inOrder.verify(mockOne, times(0)).simpleMethod(1);
try {
verify(mockOne, times(0)).simpleMethod(1);
fail();
} catch (NeverWantedButInvoked e) {}
}
@Test
public void shouldFailTimesZeroInOrder() {
inOrder.verify(mockTwo, times(2)).simpleMethod(2);
try {
inOrder.verify(mockThree, times(0)).simpleMethod(3);
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test(expected=VerificationInOrderFailure.class)
public void shouldFailWhenMockTwoWantedZeroTimes() {
inOrder.verify(mockTwo, times(0)).simpleMethod(2);
}
@Test
public void shouldVerifyLastInvocation() {
inOrder.verify(mockOne).simpleMethod(4);
}
@Test
public void shouldVerifySecondAndLastInvocation() {
inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
inOrder.verify(mockOne).simpleMethod(4);
}
@Test
public void shouldVerifySecondAndLastInvocationWhenAtLeastOnceUsed() {
inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
inOrder.verify(mockOne).simpleMethod(4);
}
@Test
public void shouldFailOnLastTwoInvocationsInWrongOrder() {
inOrder.verify(mockOne).simpleMethod(4);
try {
inOrder.verify(mockTwo, atLeastOnce()).simpleMethod(2);
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldFailOnLastAndFirstInWrongOrder() {
inOrder.verify(mockOne).simpleMethod(4);
try {
inOrder.verify(mockOne).simpleMethod(1);
fail();
} catch (VerificationInOrderFailure e) {}
}
@Test
public void shouldFailOnWrongMethodAfterLastInvocation() {
inOrder.verify(mockOne).simpleMethod(4);
try {
inOrder.verify(mockOne).simpleMethod(999);
fail();
} catch (VerificationInOrderFailure e) {}
}
}