• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors This program is made available under the terms of the MIT License.
3  */
4 
5 package org.mockitousage.verification;
6 
7 import org.junit.After;
8 import org.junit.Assert;
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.ArgumentCaptor;
14 import org.mockito.Captor;
15 import org.mockito.Mock;
16 import org.mockito.exceptions.base.MockitoAssertionError;
17 import org.mockito.junit.MockitoRule;
18 import org.mockitousage.IMethods;
19 import org.mockitoutil.RetryRule;
20 import org.mockitoutil.Stopwatch;
21 
22 import static java.util.concurrent.TimeUnit.MILLISECONDS;
23 import static junit.framework.TestCase.assertEquals;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.rules.ExpectedException.none;
26 import static org.mockito.Mockito.after;
27 import static org.mockito.Mockito.times;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.junit.MockitoJUnit.rule;
30 import static org.mockitoutil.Stopwatch.createNotStarted;
31 
32 public class VerificationAfterDelayTest {
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     @Captor
47     private ArgumentCaptor<Character> captor;
48 
49     private Stopwatch stopWatch;
50 
51     private DelayedExecution delayedExecution;
52 
53     private Runnable callMock = new Runnable() {
54         @Override
55         public void run() {
56             mock.oneArg('1');
57         }
58     };
59 
60     @Before
setUp()61     public void setUp() {
62         delayedExecution = new DelayedExecution();
63         stopWatch = createNotStarted();
64     }
65 
66     @After
tearDown()67     public void tearDown() throws InterruptedException {
68         delayedExecution.close();
69     }
70 
71     @Test
shouldVerifyNormallyWithSpecificTimes()72     public void shouldVerifyNormallyWithSpecificTimes() throws Exception {
73         // given
74         delayedExecution.callAsync(30, MILLISECONDS, callMock );
75 
76         // then
77         verify(mock, after(100).times(1)).oneArg('1');
78     }
79 
80     @Test
shouldVerifyNormallyWithAtLeast()81     public void shouldVerifyNormallyWithAtLeast() throws Exception {
82         // when
83         delayedExecution.callAsync(30, MILLISECONDS, callMock );
84 
85         // then
86         verify(mock, after(100).atLeast(1)).oneArg('1');
87     }
88 
89     @Test
shouldFailVerificationWithWrongTimes()90     public void shouldFailVerificationWithWrongTimes() throws Exception {
91         // when
92         delayedExecution.callAsync(30, MILLISECONDS, callMock );
93 
94         // then
95         verify(mock, times(0)).oneArg('1');
96         exception.expect(MockitoAssertionError.class);
97         verify(mock, after(100).times(2)).oneArg('1');
98     }
99 
100     @Test
shouldWaitTheFullTimeIfTheTestCouldPass()101     public void shouldWaitTheFullTimeIfTheTestCouldPass() throws Exception {
102         // when
103         delayedExecution.callAsync(30, MILLISECONDS, callMock );
104 
105         // then
106         stopWatch.start();
107 
108         try {
109             verify(mock, after(100).atLeast(2)).oneArg('1');
110             Assert.fail("Expected behavior was to throw an exception, and never reach this line");
111         } catch (MockitoAssertionError ignored) {
112         }
113 
114         stopWatch.assertElapsedTimeIsMoreThan(100, MILLISECONDS);
115     }
116 
117     @Test
shouldStopEarlyIfTestIsDefinitelyFailed()118     public void shouldStopEarlyIfTestIsDefinitelyFailed() throws Exception {
119         // when
120         delayedExecution.callAsync(30, MILLISECONDS, callMock );
121 
122         // then
123         exception.expect(MockitoAssertionError.class);
124         verify(mock, after(10000).never()).oneArg('1');
125     }
126 
127     /**
128      * Test for issue #345.
129      */
130     @Test
shouldReturnListOfArgumentsWithSameSizeAsGivenInAtMostVerification()131     public void shouldReturnListOfArgumentsWithSameSizeAsGivenInAtMostVerification() {
132         // given
133         int n = 3;
134 
135         // when
136         exerciseMockNTimes(n);
137 
138         stopWatch.start();
139         // then
140         verify(mock, after(200).atMost(n)).oneArg((char) captor.capture());
141 
142         stopWatch.assertElapsedTimeIsMoreThan(200, MILLISECONDS);
143         assertThat(captor.getAllValues()).containsExactly('0', '1', '2');
144     }
145 
146     @Test
shouldReturnListOfArgumentsWithSameSizeAsGivenInTimesVerification()147     public void shouldReturnListOfArgumentsWithSameSizeAsGivenInTimesVerification() {
148         // given
149         int n = 3;
150 
151         // when
152         exerciseMockNTimes(n);
153 
154         //Then
155         verify(mock, after(200).times(n)).oneArg((char) captor.capture());
156         assertEquals(n, captor.getAllValues().size());
157         assertEquals('0', (char) captor.getAllValues().get(0));
158         assertEquals('1', (char) captor.getAllValues().get(1));
159         assertEquals('2', (char) captor.getAllValues().get(2));
160     }
161 
162     @Test
shouldReturnListOfArgumentsWithSameSizeAsGivenInAtLeastVerification()163     public void shouldReturnListOfArgumentsWithSameSizeAsGivenInAtLeastVerification() {
164         // given
165         int n = 3;
166 
167         // when
168         exerciseMockNTimes(n);
169 
170         //Then
171         verify(mock, after(200).atLeast(n)).oneArg((char) captor.capture());
172         assertEquals(n, captor.getAllValues().size());
173         assertEquals('0', (char) captor.getAllValues().get(0));
174         assertEquals('1', (char) captor.getAllValues().get(1));
175         assertEquals('2', (char) captor.getAllValues().get(2));
176     }
177 
exerciseMockNTimes(int n)178     private void exerciseMockNTimes(int n) {
179         for (int i = 0; i < n; i++) {
180             mock.oneArg((char) ('0' + i));
181         }
182     }
183 
184 }
185