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.Ignore;
9 import org.junit.Rule;
10 import org.junit.Test;
11 import org.mockito.ArgumentCaptor;
12 import org.mockito.Captor;
13 import org.mockito.Mock;
14 import org.mockito.junit.MockitoRule;
15 import org.mockitousage.IMethods;
16 import org.mockitoutil.Stopwatch;
17 
18 import static java.util.concurrent.TimeUnit.MILLISECONDS;
19 import static org.assertj.core.api.Assertions.assertThat;
20 import static org.junit.Assert.assertEquals;
21 import static org.mockito.Mockito.after;
22 import static org.mockito.Mockito.verify;
23 import static org.mockito.junit.MockitoJUnit.rule;
24 import static org.mockitoutil.Stopwatch.createNotStarted;
25 
26 public class VerificationWithAfterAndCaptorTest {
27 
28     @Rule public MockitoRule mockito = rule();
29 
30     @Mock private IMethods mock;
31 
32     @Captor private ArgumentCaptor<Character> captor;
33 
34     private Stopwatch watch = createNotStarted();
35 
36     /**
37      * Test for issue #345.
38      */
39     @Test
shouldReturnListOfArgumentsWithSameSizeAsGivenInAtMostVerification()40     public void shouldReturnListOfArgumentsWithSameSizeAsGivenInAtMostVerification() {
41         // given
42         int n = 3;
43 
44         // when
45         exerciseMockNTimes(n);
46 
47         watch.start();
48 
49         // then
50         verify(mock, after(200).atMost(n)).oneArg((char) captor.capture());
51 
52         watch.assertElapsedTimeIsMoreThan(200, MILLISECONDS);
53         assertThat(captor.getAllValues()).containsExactly('0', '1', '2');
54     }
55 
56     @Test
57     @Ignore("TODO review after #936")
shouldReturnListOfArgumentsWithSameSizeAsGivenInTimesVerification()58     public void shouldReturnListOfArgumentsWithSameSizeAsGivenInTimesVerification() {
59         // given
60         int n = 3;
61 
62         // when
63         exerciseMockNTimes(n);
64 
65         //Then
66         verify(mock, after(200).times(n)).oneArg((char) captor.capture());
67         assertEquals(n, captor.getAllValues().size());
68         assertEquals('0', (char) captor.getAllValues().get(0));
69         assertEquals('1', (char) captor.getAllValues().get(1));
70         assertEquals('2', (char) captor.getAllValues().get(2));
71     }
72 
73     @Test
74     @Ignore("TODO review after #936")
shouldReturnListOfArgumentsWithSameSizeAsGivenInAtLeastVerification()75     public void shouldReturnListOfArgumentsWithSameSizeAsGivenInAtLeastVerification() {
76         // given
77         int n = 3;
78 
79         // when
80         exerciseMockNTimes(n);
81 
82         //Then
83         verify(mock, after(200).atLeast(n)).oneArg((char) captor.capture());
84         assertEquals(n, captor.getAllValues().size());
85         assertEquals('0', (char) captor.getAllValues().get(0));
86         assertEquals('1', (char) captor.getAllValues().get(1));
87         assertEquals('2', (char) captor.getAllValues().get(2));
88     }
89 
exerciseMockNTimes(int n)90     private void exerciseMockNTimes(int n) {
91         for (int i = 0; i < n; i++) {
92             mock.oneArg((char) ('0' + i));
93         }
94     }
95 }
96