• 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.After;
9 import org.junit.Before;
10 import org.junit.Rule;
11 import org.junit.Test;
12 import org.junit.rules.ExpectedException;
13 import org.mockito.InOrder;
14 import org.mockito.Mock;
15 import org.mockito.exceptions.base.MockitoAssertionError;
16 import org.mockito.exceptions.verification.NoInteractionsWanted;
17 import org.mockito.exceptions.verification.TooLittleActualInvocations;
18 import org.mockito.junit.MockitoRule;
19 import org.mockitousage.IMethods;
20 import org.mockitoutil.RetryRule;
21 
22 import static java.util.concurrent.TimeUnit.MILLISECONDS;
23 import static org.junit.rules.ExpectedException.none;
24 import static org.mockito.Mockito.inOrder;
25 import static org.mockito.Mockito.never;
26 import static org.mockito.Mockito.timeout;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.verifyNoMoreInteractions;
30 import static org.mockito.junit.MockitoJUnit.rule;
31 
32 public class VerificationWithTimeoutTest {
33 
34     @Rule
35     public MockitoRule mockito = rule();
36 
37     @Rule
38     public RetryRule retryRule = RetryRule.attempts(4);
39 
40     @Rule
41     public ExpectedException exception = none();
42 
43     @Mock
44     private IMethods mock;
45 
46     private DelayedExecution delayedExecution;
47 
48     @Before
setUp()49     public void setUp() {
50         delayedExecution = new DelayedExecution();
51     }
52 
53     @After
tearDown()54     public void tearDown() throws InterruptedException {
55         delayedExecution.close();
56     }
57 
58     @Test
shouldVerifyWithTimeout()59     public void shouldVerifyWithTimeout() throws Exception {
60         // when
61         delayedExecution.callAsync(30, MILLISECONDS, callMock('c'));
62 
63         // then
64         verify(mock, timeout(100)).oneArg('c');
65         verify(mock, timeout(100).atLeastOnce()).oneArg('c');
66         verify(mock, timeout(100).times(1)).oneArg('c');
67         verify(mock).oneArg('c');
68         verify(mock, times(1)).oneArg('c');
69     }
70 
71     @Test
shouldFailVerificationWithTimeout()72     public void shouldFailVerificationWithTimeout() throws Exception {
73         // when
74         delayedExecution.callAsync(30, MILLISECONDS, callMock('c'));
75 
76         // then
77         verify(mock, never()).oneArg('c');
78         exception.expect(MockitoAssertionError.class);
79         verify(mock, timeout(20).atLeastOnce()).oneArg('c');
80     }
81 
82     @Test
shouldAllowMixingOtherModesWithTimeout()83     public void shouldAllowMixingOtherModesWithTimeout() throws Exception {
84         // when
85         delayedExecution.callAsync(10, MILLISECONDS, callMock('c'));
86         delayedExecution.callAsync(10, MILLISECONDS, callMock('c'));
87 
88         // then
89         verify(mock, timeout(100).atLeast(1)).oneArg('c');
90         verify(mock, timeout(100).times(2)).oneArg('c');
91         verifyNoMoreInteractions(mock);
92     }
93 
94     @Test
shouldAllowMixingOtherModesWithTimeoutAndFail()95     public void shouldAllowMixingOtherModesWithTimeoutAndFail() throws Exception {
96         // when
97         delayedExecution.callAsync(10, MILLISECONDS, callMock('c'));
98         delayedExecution.callAsync(10, MILLISECONDS, callMock('c'));
99 
100         // then
101         verify(mock, timeout(100).atLeast(1)).oneArg('c');
102         exception.expect(TooLittleActualInvocations.class);
103         verify(mock, timeout(100).times(3)).oneArg('c');
104     }
105 
106     @Test
shouldAllowMixingOnlyWithTimeout()107     public void shouldAllowMixingOnlyWithTimeout() throws Exception {
108         // when
109         delayedExecution.callAsync(30, MILLISECONDS, callMock('c'));
110 
111         // then
112         verify(mock, never()).oneArg('c');
113         verify(mock, timeout(100).only()).oneArg('c');
114     }
115 
116     @Test
shouldAllowMixingOnlyWithTimeoutAndFail()117     public void shouldAllowMixingOnlyWithTimeoutAndFail() throws Exception {
118         // when
119         delayedExecution.callAsync(30, MILLISECONDS, callMock('c'));
120 
121         // and when
122         mock.oneArg('x');
123 
124         // then
125         verify(mock, never()).oneArg('c');
126         exception.expect(NoInteractionsWanted.class);
127         verify(mock, timeout(100).only()).oneArg('c');
128     }
129 
130     /**
131      * This test is JUnit-specific because the code behaves different if JUnit
132      * is used.
133      */
134     @Test
canIgnoreInvocationsWithJunit()135     public void canIgnoreInvocationsWithJunit() throws InterruptedException {
136         // when
137         delayedExecution.callAsync(10, MILLISECONDS, callMock('1'));
138 
139         // then
140         verify(mock, timeout(50)).oneArg('1');
141 
142         // when
143         delayedExecution.callAsync(10, MILLISECONDS, callMock('2'));
144         delayedExecution.callAsync(20, MILLISECONDS, callMock('3'));
145 
146         // then
147         verify(mock, timeout(50)).oneArg('3');
148     }
149 
150     @Test
shouldAllowTimeoutVerificationInOrder()151     public void shouldAllowTimeoutVerificationInOrder() throws Exception {
152         // when
153         delayedExecution.callAsync(50, MILLISECONDS, callMock('1'));
154 
155         // and when
156         mock.oneArg('x');
157 
158         // then
159         InOrder inOrder = inOrder(mock);
160         inOrder.verify(mock).oneArg('x');
161         inOrder.verify(mock, never()).oneArg('1');
162         inOrder.verify(mock, timeout(100)).oneArg('1');
163     }
164 
callMock(final char c)165     private Runnable callMock(final char c) {
166         return new Runnable() {
167             @Override
168             public void run() {
169                 mock.oneArg(c);
170             }
171         };
172     }
173 }
174