1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.verification; 6 7 import org.assertj.core.api.Assertions; 8 import org.junit.Test; 9 import org.mockito.exceptions.verification.NoInteractionsWanted; 10 import org.mockito.exceptions.verification.VerificationInOrderFailure; 11 import org.mockito.internal.creation.MockSettingsImpl; 12 import org.mockito.internal.invocation.InvocationBuilder; 13 import org.mockito.internal.invocation.InvocationMatcher; 14 import org.mockito.internal.stubbing.InvocationContainerImpl; 15 import org.mockito.internal.verification.api.VerificationDataInOrderImpl; 16 import org.mockito.invocation.Invocation; 17 import org.mockitousage.IMethods; 18 import org.mockitoutil.TestBase; 19 20 import static java.util.Arrays.asList; 21 import static junit.framework.TestCase.*; 22 import static org.mockito.Mockito.mock; 23 24 public class NoMoreInteractionsTest extends TestBase { 25 26 InOrderContextImpl context = new InOrderContextImpl(); 27 28 @Test 29 public void shouldVerifyInOrder() { 30 //given 31 NoMoreInteractions n = new NoMoreInteractions(); 32 Invocation i = new InvocationBuilder().toInvocation(); 33 assertFalse(context.isVerified(i)); 34 35 try { 36 //when 37 n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i), null)); 38 //then 39 fail(); 40 } catch(VerificationInOrderFailure e) {} 41 } 42 43 @Test 44 public void shouldVerifyInOrderAndPass() { 45 //given 46 NoMoreInteractions n = new NoMoreInteractions(); 47 Invocation i = new InvocationBuilder().toInvocation(); 48 context.markVerified(i); 49 assertTrue(context.isVerified(i)); 50 51 //when 52 n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i), null)); 53 //then no exception is thrown 54 } 55 56 @Test 57 public void shouldVerifyInOrderMultipleInvoctions() { 58 //given 59 NoMoreInteractions n = new NoMoreInteractions(); 60 Invocation i = new InvocationBuilder().seq(1).toInvocation(); 61 Invocation i2 = new InvocationBuilder().seq(2).toInvocation(); 62 63 //when 64 context.markVerified(i2); 65 66 //then no exception is thrown 67 n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i, i2), null)); 68 } 69 70 @Test 71 public void shouldVerifyInOrderMultipleInvoctionsAndThrow() { 72 //given 73 NoMoreInteractions n = new NoMoreInteractions(); 74 Invocation i = new InvocationBuilder().seq(1).toInvocation(); 75 Invocation i2 = new InvocationBuilder().seq(2).toInvocation(); 76 77 try { 78 //when 79 n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i, i2), null)); 80 fail(); 81 } catch (VerificationInOrderFailure e) {} 82 } 83 84 @Test 85 public void noMoreInteractionsExceptionMessageShouldDescribeMock() { 86 //given 87 NoMoreInteractions n = new NoMoreInteractions(); 88 IMethods mock = mock(IMethods.class, "a mock"); 89 InvocationMatcher i = new InvocationBuilder().mock(mock).toInvocationMatcher(); 90 91 InvocationContainerImpl invocations = 92 new InvocationContainerImpl( new MockSettingsImpl()); 93 invocations.setInvocationForPotentialStubbing(i); 94 95 try { 96 //when 97 n.verify(new VerificationDataImpl(invocations, null)); 98 //then 99 fail(); 100 } catch (NoInteractionsWanted e) { 101 Assertions.assertThat(e.toString()).contains(mock.toString()); 102 } 103 } 104 105 @Test 106 public void noMoreInteractionsInOrderExceptionMessageShouldDescribeMock() { 107 //given 108 NoMoreInteractions n = new NoMoreInteractions(); 109 IMethods mock = mock(IMethods.class, "a mock"); 110 Invocation i = new InvocationBuilder().mock(mock).toInvocation(); 111 112 try { 113 //when 114 n.verifyInOrder(new VerificationDataInOrderImpl(context, asList(i), null)); 115 //then 116 fail(); 117 } catch (VerificationInOrderFailure e) { 118 Assertions.assertThat(e.toString()).contains(mock.toString()); 119 } 120 } 121 } 122