forked from mockito/mockito
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CapturingArgumentsTest.java
331 lines (259 loc) · 10.2 KB
/
CapturingArgumentsTest.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.matchers;
import org.assertj.core.api.Assertions;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.exceptions.verification.WantedButNotInvoked;
import org.mockitousage.IMethods;
import org.mockitoutil.TestBase;
import java.util.ArrayList;
import java.util.List;
import static junit.framework.TestCase.*;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
public class CapturingArgumentsTest extends TestBase {
class Person {
private final Integer age;
public Person(Integer age) {
this.age = age;
}
public int getAge() {
return age;
}
}
class BulkEmailService {
private EmailService service;
public BulkEmailService(EmailService service) {
this.service = service;
}
public void email(Integer ... personId) {
for (Integer i : personId) {
Person person = new Person(i);
service.sendEmailTo(person);
}
}
}
interface EmailService {
boolean sendEmailTo(Person person);
}
EmailService emailService = mock(EmailService.class);
BulkEmailService bulkEmailService = new BulkEmailService(emailService);
IMethods mock = mock(IMethods.class);
@SuppressWarnings("deprecation")
@Test
public void should_allow_assertions_on_captured_argument() {
//given
ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
//when
bulkEmailService.email(12);
//then
verify(emailService).sendEmailTo(argument.capture());
assertEquals(12, argument.getValue().getAge());
}
@Test
public void should_allow_assertions_on_all_captured_arguments() {
//given
ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
//when
bulkEmailService.email(11, 12);
//then
verify(emailService, times(2)).sendEmailTo(argument.capture());
assertEquals(11, argument.getAllValues().get(0).getAge());
assertEquals(12, argument.getAllValues().get(1).getAge());
}
@Test
public void should_allow_assertions_on_last_argument() {
//given
ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
//when
bulkEmailService.email(11, 12, 13);
//then
verify(emailService, times(3)).sendEmailTo(argument.capture());
assertEquals(13, argument.getValue().getAge());
}
@Test
public void should_print_captor_matcher() {
//given
ArgumentCaptor<Person> person = ArgumentCaptor.forClass(Person.class);
try {
//when
verify(emailService).sendEmailTo(person.capture());
fail();
} catch(WantedButNotInvoked e) {
//then
assertThat(e).hasMessageContaining("<Capturing argument>");
}
}
@Test
public void should_allow_assertions_on_captured_null() {
//given
ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
//when
emailService.sendEmailTo(null);
//then
verify(emailService).sendEmailTo(argument.capture());
assertEquals(null, argument.getValue());
}
@Test
public void should_allow_construction_of_captor_for_parameterized_type_in_a_convenient_way() {
//the test passes if this expression compiles
@SuppressWarnings("unchecked")
ArgumentCaptor<List<Person>> argument = ArgumentCaptor.forClass(List.class);
assertNotNull(argument);
}
@Test
public void should_allow_construction_of_captor_for_a_more_specific_type() {
//the test passes if this expression compiles
ArgumentCaptor<List<?>> argument = ArgumentCaptor.forClass(ArrayList.class);
assertNotNull(argument);
}
@Test
public void should_allow_capturing_for_stubbing() {
//given
ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
when(emailService.sendEmailTo(argument.capture())).thenReturn(false);
//when
emailService.sendEmailTo(new Person(10));
//then
assertEquals(10, argument.getValue().getAge());
}
@Test
public void should_capture_when_stubbing_only_when_entire_invocation_matches() {
//given
ArgumentCaptor<String> argument = ArgumentCaptor.forClass(String.class);
when(mock.simpleMethod(argument.capture(), eq(2))).thenReturn("blah");
//when
mock.simpleMethod("foo", 200);
mock.simpleMethod("bar", 2);
//then
Assertions.assertThat(argument.getAllValues()).containsOnly("bar");
}
@Test
public void should_say_something_smart_when_misused() {
ArgumentCaptor<Person> argument = ArgumentCaptor.forClass(Person.class);
try {
argument.getValue();
fail();
} catch (MockitoException expected) { }
}
@Test
public void should_capture_when_full_arg_list_matches() throws Exception {
//given
ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class);
//when
mock.simpleMethod("foo", 1);
mock.simpleMethod("bar", 2);
//then
verify(mock).simpleMethod(captor.capture(), eq(1));
assertEquals(1, captor.getAllValues().size());
assertEquals("foo", captor.getValue());
}
@Test
public void should_capture_int_by_creating_captor_with_primitive_wrapper() {
//given
ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(Integer.class);
//when
mock.intArgumentMethod(10);
//then
verify(mock).intArgumentMethod(argument.capture());
assertEquals(10, (int) argument.getValue());
}
@Test
public void should_capture_int_by_creating_captor_with_primitive() throws Exception {
//given
ArgumentCaptor<Integer> argument = ArgumentCaptor.forClass(int.class);
//when
mock.intArgumentMethod(10);
//then
verify(mock).intArgumentMethod(argument.capture());
assertEquals(10, (int) argument.getValue());
}
@Test
public void should_capture_byte_vararg_by_creating_captor_with_primitive() throws Exception {
// given
ArgumentCaptor<Byte> argumentCaptor = ArgumentCaptor.forClass(byte.class);
// when
mock.varargsbyte((byte) 1, (byte) 2);
// then
verify(mock).varargsbyte(argumentCaptor.capture());
assertEquals((byte) 2, (byte) argumentCaptor.getValue());
Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly((byte) 1, (byte) 2);
}
@Test
public void should_capture_byte_vararg_by_creating_captor_with_primitive_wrapper() throws Exception {
// given
ArgumentCaptor<Byte> argumentCaptor = ArgumentCaptor.forClass(Byte.class);
// when
mock.varargsbyte((byte) 1, (byte) 2);
// then
verify(mock).varargsbyte(argumentCaptor.capture());
assertEquals((byte) 2, (byte) argumentCaptor.getValue());
Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly((byte) 1, (byte) 2);
}
@Test
public void should_capture_vararg() throws Exception {
// given
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
// when
mock.mixedVarargs(42, "a", "b", "c");
// then
verify(mock).mixedVarargs(any(), argumentCaptor.capture());
Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly("a", "b", "c");
}
@Test
public void should_capture_all_vararg() throws Exception {
// given
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
// when
mock.mixedVarargs(42, "a", "b", "c");
mock.mixedVarargs(42, "again ?!");
// then
verify(mock, times(2)).mixedVarargs(any(), argumentCaptor.capture());
Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly("a", "b", "c", "again ?!");
}
@Test
public void should_capture_one_arg_even_when_using_vararg_captor_on_nonvararg_method() throws Exception {
// given
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
// when
mock.simpleMethod("a", 2);
// then
verify(mock).simpleMethod(argumentCaptor.capture(), eq(2));
Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly("a");
}
@Test
public void captures_correctly_when_captor_used_multiple_times() throws Exception {
// given
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
// when
mock.mixedVarargs(42, "a", "b", "c");
// then
// this is only for backwards compatibility. It does not make sense in real to do so.
verify(mock).mixedVarargs(any(), argumentCaptor.capture(), argumentCaptor.capture(), argumentCaptor.capture());
Assertions.assertThat(argumentCaptor.getAllValues()).containsExactly("a", "b", "c");
}
@Test
public void captures_correctly_when_captor_used_on_pure_vararg_method() throws Exception {
// given
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
// when
mock.varargs(42, "capturedValue");
// then
verify(mock).varargs(eq(42), argumentCaptor.capture());
Assertions.assertThat(argumentCaptor.getValue()).contains("capturedValue");
}
@Test
public void should_capture_null_vararg() {
// given
ArgumentCaptor<String> argumentCaptor = ArgumentCaptor.forClass(String.class);
// when
mock.varargs(null);
// then
verify(mock).varargs(argumentCaptor.capture());
Assertions.assertThat(argumentCaptor.getValue()).isNull();
}
}