forked from mockito/mockito
/
StubbingWithAdditionalAnswersTest.java
455 lines (386 loc) · 19.3 KB
/
StubbingWithAdditionalAnswersTest.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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage.stubbing;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.within;
import static org.mockito.AdditionalAnswers.answer;
import static org.mockito.AdditionalAnswers.answerVoid;
import static org.mockito.AdditionalAnswers.answersWithDelay;
import static org.mockito.AdditionalAnswers.returnsArgAt;
import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.AdditionalAnswers.returnsLastArg;
import static org.mockito.AdditionalAnswers.returnsSecondArg;
import static org.mockito.BDDMockito.any;
import static org.mockito.BDDMockito.anyInt;
import static org.mockito.BDDMockito.anyString;
import static org.mockito.BDDMockito.eq;
import static org.mockito.BDDMockito.given;
import static org.mockito.BDDMockito.mock;
import static org.mockito.BDDMockito.times;
import static org.mockito.BDDMockito.verify;
import java.util.Date;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.stubbing.Answer1;
import org.mockito.stubbing.Answer2;
import org.mockito.stubbing.Answer3;
import org.mockito.stubbing.Answer4;
import org.mockito.stubbing.Answer5;
import org.mockito.stubbing.Answer6;
import org.mockito.stubbing.VoidAnswer1;
import org.mockito.stubbing.VoidAnswer2;
import org.mockito.stubbing.VoidAnswer3;
import org.mockito.stubbing.VoidAnswer4;
import org.mockito.stubbing.VoidAnswer5;
import org.mockito.stubbing.VoidAnswer6;
import org.mockitousage.IMethods;
@RunWith(MockitoJUnitRunner.class)
public class StubbingWithAdditionalAnswersTest {
@Mock IMethods iMethods;
@Test
public void can_return_arguments_of_invocation() throws Exception {
given(iMethods.objectArgMethod(any())).will(returnsFirstArg());
given(iMethods.threeArgumentMethod(eq(0), any(), anyString())).will(returnsSecondArg());
given(iMethods.threeArgumentMethod(eq(1), any(), anyString())).will(returnsLastArg());
given(iMethods.mixedVarargsReturningString(eq(1), any())).will(returnsArgAt(2));
assertThat(iMethods.objectArgMethod("first")).isEqualTo("first");
assertThat(iMethods.threeArgumentMethod(0, "second", "whatever")).isEqualTo("second");
assertThat(iMethods.threeArgumentMethod(1, "whatever", "last")).isEqualTo("last");
assertThat(iMethods.mixedVarargsReturningString(1, "a", "b")).isEqualTo("b");
}
@Test
public void can_return_var_arguments_of_invocation() throws Exception {
given(iMethods.mixedVarargsReturningStringArray(eq(1), any())).will(returnsLastArg());
given(iMethods.mixedVarargsReturningObjectArray(eq(1), any())).will(returnsArgAt(1));
assertThat(iMethods.mixedVarargsReturningStringArray(1, "the", "var", "args"))
.containsExactlyInAnyOrder("the", "var", "args");
assertThat(iMethods.mixedVarargsReturningObjectArray(1, "the", "var", "args"))
.containsExactlyInAnyOrder("the", "var", "args");
}
@Test
public void returns_arg_at_throws_on_out_of_range_var_args() throws Exception {
given(iMethods.mixedVarargsReturningString(eq(1), any())).will(returnsArgAt(3));
assertThatThrownBy(() -> iMethods.mixedVarargsReturningString(1, "a", "b"))
.isInstanceOf(MockitoException.class)
.hasMessageContaining("Invalid argument index");
assertThatThrownBy(
() ->
given(iMethods.mixedVarargsReturningStringArray(eq(1), any()))
.will(returnsArgAt(3)))
.isInstanceOf(MockitoException.class)
.hasMessageContaining("The argument of type 'String' cannot be returned");
}
@Test
public void can_return_after_delay() throws Exception {
final long sleepyTime = 500L;
given(iMethods.objectArgMethod(any()))
.will(answersWithDelay(sleepyTime, returnsFirstArg()));
final Date before = new Date();
assertThat(iMethods.objectArgMethod("first")).isEqualTo("first");
final Date after = new Date();
final long timePassed = after.getTime() - before.getTime();
assertThat(timePassed).isCloseTo(sleepyTime, within(15L));
}
@Test
public void can_return_expanded_arguments_of_invocation() throws Exception {
given(iMethods.varargsObject(eq(1), any())).will(returnsArgAt(3));
assertThat(iMethods.varargsObject(1, "bob", "alexander", "alice", "carl"))
.isEqualTo("alice");
}
@Test
public void can_return_primitives_or_wrappers() throws Exception {
given(iMethods.toIntPrimitive(anyInt())).will(returnsFirstArg());
given(iMethods.toIntWrapper(anyInt())).will(returnsFirstArg());
assertThat(iMethods.toIntPrimitive(1)).isEqualTo(1);
assertThat(iMethods.toIntWrapper(1)).isEqualTo(1);
}
@Test
public void can_return_based_on_strongly_types_one_parameter_function() throws Exception {
given(iMethods.simpleMethod(anyString()))
.will(
answer(
new Answer1<String, String>() {
public String answer(String s) {
return s;
}
}));
assertThat(iMethods.simpleMethod("string")).isEqualTo("string");
}
@Test
public void will_execute_a_void_based_on_strongly_typed_one_parameter_function()
throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.simpleMethod(anyString()))
.will(
answerVoid(
new VoidAnswer1<String>() {
public void answer(String s) {
target.simpleMethod(s);
}
}));
// invoke on iMethods
iMethods.simpleMethod("string");
// expect the answer to write correctly to "target"
verify(target, times(1)).simpleMethod("string");
}
@Test
public void can_return_based_on_strongly_typed_two_parameter_function() throws Exception {
given(iMethods.simpleMethod(anyString(), anyInt()))
.will(
answer(
new Answer2<String, String, Integer>() {
public String answer(String s, Integer i) {
return s + "-" + i;
}
}));
assertThat(iMethods.simpleMethod("string", 1)).isEqualTo("string-1");
}
@Test
public void will_execute_a_void_based_on_strongly_typed_two_parameter_function()
throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.simpleMethod(anyString(), anyInt()))
.will(
answerVoid(
new VoidAnswer2<String, Integer>() {
public void answer(String s, Integer i) {
target.simpleMethod(s, i);
}
}));
// invoke on iMethods
iMethods.simpleMethod("string", 1);
// expect the answer to write correctly to "target"
verify(target, times(1)).simpleMethod("string", 1);
}
@Test
public void can_return_based_on_strongly_typed_three_parameter_function() throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.threeArgumentMethodWithStrings(anyInt(), anyString(), anyString()))
.will(
answer(
new Answer3<String, Integer, String, String>() {
public String answer(Integer i, String s1, String s2) {
target.threeArgumentMethodWithStrings(i, s1, s2);
return "answered";
}
}));
assertThat(iMethods.threeArgumentMethodWithStrings(1, "string1", "string2"))
.isEqualTo("answered");
verify(target, times(1)).threeArgumentMethodWithStrings(1, "string1", "string2");
}
@Test
public void will_execute_a_void_based_on_strongly_typed_three_parameter_function()
throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.threeArgumentMethodWithStrings(anyInt(), anyString(), anyString()))
.will(
answerVoid(
new VoidAnswer3<Integer, String, String>() {
public void answer(Integer i, String s1, String s2) {
target.threeArgumentMethodWithStrings(i, s1, s2);
}
}));
// invoke on iMethods
iMethods.threeArgumentMethodWithStrings(1, "string1", "string2");
// expect the answer to write correctly to "target"
verify(target, times(1)).threeArgumentMethodWithStrings(1, "string1", "string2");
}
@Test
public void can_return_based_on_strongly_typed_four_parameter_function() throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.fourArgumentMethod(anyInt(), anyString(), anyString(), any(boolean[].class)))
.will(
answer(
new Answer4<String, Integer, String, String, boolean[]>() {
public String answer(
Integer i, String s1, String s2, boolean[] a) {
target.fourArgumentMethod(i, s1, s2, a);
return "answered";
}
}));
boolean[] booleanArray = {true, false};
assertThat(iMethods.fourArgumentMethod(1, "string1", "string2", booleanArray))
.isEqualTo("answered");
verify(target, times(1)).fourArgumentMethod(1, "string1", "string2", booleanArray);
}
@Test
public void will_execute_a_void_based_on_strongly_typed_four_parameter_function()
throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.fourArgumentMethod(anyInt(), anyString(), anyString(), any(boolean[].class)))
.will(
answerVoid(
new VoidAnswer4<Integer, String, String, boolean[]>() {
public void answer(
Integer i, String s1, String s2, boolean[] a) {
target.fourArgumentMethod(i, s1, s2, a);
}
}));
// invoke on iMethods
boolean[] booleanArray = {true, false};
iMethods.fourArgumentMethod(1, "string1", "string2", booleanArray);
// expect the answer to write correctly to "target"
verify(target, times(1)).fourArgumentMethod(1, "string1", "string2", booleanArray);
}
@Test
public void can_return_based_on_strongly_typed_five_parameter_function() throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.simpleMethod(anyString(), anyInt(), anyInt(), anyInt(), anyInt()))
.will(
answer(
new Answer5<String, String, Integer, Integer, Integer, Integer>() {
public String answer(
String s1,
Integer i1,
Integer i2,
Integer i3,
Integer i4) {
target.simpleMethod(s1, i1, i2, i3, i4);
return "answered";
}
}));
assertThat(iMethods.simpleMethod("hello", 1, 2, 3, 4)).isEqualTo("answered");
verify(target, times(1)).simpleMethod("hello", 1, 2, 3, 4);
}
@Test
public void will_execute_a_void_based_on_strongly_typed_five_parameter_function()
throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.simpleMethod(anyString(), anyInt(), anyInt(), anyInt(), anyInt()))
.will(
answerVoid(
new VoidAnswer5<String, Integer, Integer, Integer, Integer>() {
public void answer(
String s1,
Integer i1,
Integer i2,
Integer i3,
Integer i4) {
target.simpleMethod(s1, i1, i2, i3, i4);
}
}));
// invoke on iMethods
iMethods.simpleMethod("hello", 1, 2, 3, 4);
// expect the answer to write correctly to "target"
verify(target, times(1)).simpleMethod("hello", 1, 2, 3, 4);
}
@Test
public void can_return_based_on_strongly_typed_six_parameter_function() throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.simpleMethod(anyString(), anyInt(), anyInt(), anyInt(), anyInt(), anyInt()))
.will(
answer(
new Answer6<
String,
String,
Integer,
Integer,
Integer,
Integer,
Integer>() {
public String answer(
String s1,
Integer i1,
Integer i2,
Integer i3,
Integer i4,
Integer i5) {
target.simpleMethod(s1, i1, i2, i3, i4, i5);
return "answered";
}
}));
assertThat(iMethods.simpleMethod("hello", 1, 2, 3, 4, 5)).isEqualTo("answered");
verify(target, times(1)).simpleMethod("hello", 1, 2, 3, 4, 5);
}
@Test
public void will_execute_a_void_returning_strongly_typed_six_parameter_function()
throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.simpleMethod(anyString(), anyInt(), anyInt(), anyInt(), anyInt(), anyInt()))
.will(
answerVoid(
new VoidAnswer6<
String, Integer, Integer, Integer, Integer, Integer>() {
public void answer(
String s1,
Integer i1,
Integer i2,
Integer i3,
Integer i4,
Integer i5) {
target.simpleMethod(s1, i1, i2, i3, i4, i5);
}
}));
// invoke on iMethods
iMethods.simpleMethod("hello", 1, 2, 3, 4, 5);
// expect the answer to write correctly to "target"
verify(target, times(1)).simpleMethod("hello", 1, 2, 3, 4, 5);
}
@Test
public void can_return_based_on_strongly_types_one_parameter_var_args_function()
throws Exception {
given(iMethods.varargs(any()))
.will(
answer(
new Answer1<Integer, String[]>() {
public Integer answer(String[] strings) {
return strings.length;
}
}));
assertThat(iMethods.varargs("some", "args")).isEqualTo(2);
}
@Test
public void will_execute_a_void_based_on_strongly_typed_one_parameter_var_args_function()
throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.varargs(any()))
.will(
answerVoid(
new VoidAnswer1<String[]>() {
public void answer(String[] s) {
target.varargs(s);
}
}));
// invoke on iMethods
iMethods.varargs("some", "args");
// expect the answer to write correctly to "target"
verify(target, times(1)).varargs("some", "args");
}
@Test
public void can_return_based_on_strongly_typed_two_parameter_var_args_function()
throws Exception {
given(iMethods.mixedVarargsReturningString(any(), any()))
.will(
answer(
new Answer2<String, Object, String[]>() {
public String answer(Object o, String[] s) {
return String.join("-", s);
}
}));
assertThat(iMethods.mixedVarargsReturningString(1, "var", "args")).isEqualTo("var-args");
}
@Test
public void will_execute_a_void_based_on_strongly_typed_two_parameter_var_args_function()
throws Exception {
final IMethods target = mock(IMethods.class);
given(iMethods.mixedVarargsReturningString(any(), any()))
.will(
answerVoid(
new VoidAnswer2<Object, String[]>() {
public void answer(Object o, String[] s) {
target.mixedVarargsReturningString(o, s);
}
}));
// invoke on iMethods
iMethods.mixedVarargsReturningString(1, "var", "args");
// expect the answer to write correctly to "target"
verify(target, times(1)).mixedVarargsReturningString(1, "var", "args");
}
// todo: tests for other versions of answer and voidAnswer.
}