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