• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockitousage.verification;
7 
8 import org.junit.Test;
9 import org.mockito.InOrder;
10 import org.mockito.Mock;
11 import org.mockito.exceptions.misusing.UnfinishedVerificationException;
12 import org.mockito.exceptions.verification.VerificationInOrderFailure;
13 import org.mockitousage.IMethods;
14 import org.mockitoutil.TestBase;
15 
16 import static junit.framework.TestCase.fail;
17 import static org.assertj.core.api.Assertions.assertThat;
18 import static org.mockito.Mockito.inOrder;
19 import static org.mockito.Mockito.verify;
20 
21 public class FindingRedundantInvocationsInOrderTest extends TestBase {
22 
23     @Mock private IMethods mock;
24     @Mock private IMethods mock2;
25 
26     @Test
shouldWorkFineIfNoInvocatins()27     public void shouldWorkFineIfNoInvocatins() throws Exception {
28         //when
29         InOrder inOrder = inOrder(mock);
30 
31         //then
32         inOrder.verifyNoMoreInteractions();
33     }
34 
35     @Test
shouldSayNoInteractionsWanted()36     public void shouldSayNoInteractionsWanted() throws Exception {
37         //when
38         mock.simpleMethod();
39 
40         //then
41         InOrder inOrder = inOrder(mock);
42         try {
43             inOrder.verifyNoMoreInteractions();
44             fail();
45         } catch(VerificationInOrderFailure e) {
46             assertThat(e).hasMessageContaining("No interactions wanted");
47         }
48     }
49 
50     @Test
shouldVerifyNoMoreInteractionsInOrder()51     public void shouldVerifyNoMoreInteractionsInOrder() throws Exception {
52         //when
53         mock.simpleMethod();
54         mock.simpleMethod(10);
55         mock.otherMethod();
56 
57         //then
58         InOrder inOrder = inOrder(mock);
59         inOrder.verify(mock).simpleMethod(10);
60         inOrder.verify(mock).otherMethod();
61         inOrder.verifyNoMoreInteractions();
62     }
63 
64     @Test
shouldVerifyNoMoreInteractionsInOrderWithMultipleMocks()65     public void shouldVerifyNoMoreInteractionsInOrderWithMultipleMocks() throws Exception {
66         //when
67         mock.simpleMethod();
68         mock2.simpleMethod();
69         mock.otherMethod();
70 
71         //then
72         InOrder inOrder = inOrder(mock, mock2);
73         inOrder.verify(mock2).simpleMethod();
74         inOrder.verify(mock).otherMethod();
75         inOrder.verifyNoMoreInteractions();
76     }
77 
78     @Test
shouldFailToVerifyNoMoreInteractionsInOrder()79     public void shouldFailToVerifyNoMoreInteractionsInOrder() throws Exception {
80         //when
81         mock.simpleMethod();
82         mock.simpleMethod(10);
83         mock.otherMethod();
84 
85         //then
86         InOrder inOrder = inOrder(mock);
87         inOrder.verify(mock).simpleMethod(10);
88         try {
89             inOrder.verifyNoMoreInteractions();
90             fail();
91         } catch(VerificationInOrderFailure e) {}
92     }
93 
94     @Test
shouldFailToVerifyNoMoreInteractionsInOrderWithMultipleMocks()95     public void shouldFailToVerifyNoMoreInteractionsInOrderWithMultipleMocks() throws Exception {
96         //when
97         mock.simpleMethod();
98         mock2.simpleMethod();
99         mock.otherMethod();
100 
101         //then
102         InOrder inOrder = inOrder(mock, mock2);
103         inOrder.verify(mock2).simpleMethod();
104         try {
105             inOrder.verifyNoMoreInteractions();
106             fail();
107         } catch(VerificationInOrderFailure e) {}
108     }
109 
110     @Test
shouldValidateState()111     public void shouldValidateState() throws Exception {
112         //when
113         InOrder inOrder = inOrder(mock);
114         verify(mock); // mess up state
115 
116         //then
117         try {
118             inOrder.verifyNoMoreInteractions();
119             fail();
120         } catch(UnfinishedVerificationException e) {}
121     }
122 }
123