forked from mockito/mockito
/
ReturnsSmartNullsTest.java
373 lines (288 loc) · 13.3 KB
/
ReturnsSmartNullsTest.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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.internal.stubbing.defaultanswers;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.assertj.core.api.Assertions;
import org.assertj.core.api.ThrowableAssert;
import org.junit.Test;
import org.mockito.exceptions.verification.SmartNullPointerException;
import org.mockito.internal.debugging.LocationImpl;
import org.mockito.internal.invocation.InterceptedInvocation;
import org.mockito.internal.invocation.SerializableMethod;
import org.mockito.internal.invocation.mockref.MockStrongReference;
import org.mockito.stubbing.Answer;
import org.mockitoutil.TestBase;
public class ReturnsSmartNullsTest extends TestBase {
@Test
public void should_return_the_usual_default_values_for_primitives() throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
assertEquals(
false, answer.answer(invocationOf(HasPrimitiveMethods.class, "booleanMethod")));
assertEquals(
(char) 0, answer.answer(invocationOf(HasPrimitiveMethods.class, "charMethod")));
assertEquals(
(byte) 0, answer.answer(invocationOf(HasPrimitiveMethods.class, "byteMethod")));
assertEquals(
(short) 0, answer.answer(invocationOf(HasPrimitiveMethods.class, "shortMethod")));
assertEquals(0, answer.answer(invocationOf(HasPrimitiveMethods.class, "intMethod")));
assertEquals(0L, answer.answer(invocationOf(HasPrimitiveMethods.class, "longMethod")));
assertEquals(0f, answer.answer(invocationOf(HasPrimitiveMethods.class, "floatMethod")));
assertEquals(0d, answer.answer(invocationOf(HasPrimitiveMethods.class, "doubleMethod")));
}
@SuppressWarnings("unused")
interface Foo {
Foo get();
Foo withArgs(String oneArg, String otherArg);
}
@Test
public void should_return_an_object_that_fails_on_any_method_invocation_for_non_primitives()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "get"));
try {
smartNull.get();
fail();
} catch (SmartNullPointerException expected) {
}
}
@Test
public void should_return_an_object_that_allows_object_methods() throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "get"));
assertThat(smartNull.toString()).contains("SmartNull returned by").contains("foo.get()");
}
@Test
public void should_print_the_parameters_when_calling_a_method_with_args() throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "withArgs", "oompa", "lumpa"));
assertThat(smartNull.toString())
.contains("foo.withArgs")
.contains("oompa")
.contains("lumpa");
}
@Test
public void should_print_the_parameters_on_SmartNullPointerException_message()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
Foo smartNull = (Foo) answer.answer(invocationOf(Foo.class, "withArgs", "oompa", "lumpa"));
try {
smartNull.get();
fail();
} catch (SmartNullPointerException e) {
assertThat(e).hasMessageContaining("oompa").hasMessageContaining("lumpa");
}
}
interface GenericFoo<T> {
T get();
}
interface GenericFooBar extends GenericFoo<Foo> {
<I> I method();
<I> I methodWithArgs(int firstArg, I secondArg);
<I> I methodWithVarArgs(int firstArg, I... secondArg);
}
@Test
public void should_return_an_object_that_has_been_defined_with_class_generic()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
Foo smartNull = (Foo) answer.answer(invocationOf(GenericFooBar.class, "get"));
assertThat(smartNull.toString())
.contains("SmartNull returned by")
.contains("genericFooBar.get()");
}
@Test
public void should_return_an_object_that_has_been_defined_with_method_generic()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
String smartNull = (String) answer.answer(invocationOf(GenericFooBar.class, "method"));
assertThat(smartNull).isNull();
}
private static <T> InterceptedInvocation invocationMethodWithArgs(final T obj)
throws NoSuchMethodException {
return new InterceptedInvocation(
new MockStrongReference<Object>(mock(GenericFooBar.class), false),
new SerializableMethod(
GenericFooBar.class.getMethod("methodWithArgs", int.class, Object.class)),
new Object[] {1, obj},
InterceptedInvocation.NO_OP,
new LocationImpl(),
1);
}
@Test
public void
should_return_a_String_that_has_been_defined_with_method_generic_and_provided_in_argument()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
Object smartNull = answer.answer(invocationMethodWithArgs("secondArg"));
assertThat(smartNull).isNotNull().isInstanceOf(String.class).asString().isEmpty();
}
@Test
public void
should_return_a_empty_list_that_has_been_defined_with_method_generic_and_provided_in_argument()
throws Throwable {
final List<String> list = Collections.singletonList("String");
Answer<Object> answer = new ReturnsSmartNulls();
Object smartNull = answer.answer(invocationMethodWithArgs(list));
assertThat(smartNull).isNotNull().isInstanceOf(List.class);
assertThat((List) smartNull).isEmpty();
}
@Test
public void
should_return_a_empty_map_that_has_been_defined_with_method_generic_and_provided_in_argument()
throws Throwable {
final Map<String, String> map = new HashMap<String, String>();
map.put("key-1", "value-1");
map.put("key-2", "value-2");
Answer<Object> answer = new ReturnsSmartNulls();
Object smartNull = answer.answer(invocationMethodWithArgs(map));
assertThat(smartNull).isNotNull().isInstanceOf(Map.class);
assertThat((Map) smartNull).isEmpty();
}
@Test
public void
should_return_a_empty_set_that_has_been_defined_with_method_generic_and_provided_in_argument()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
Object smartNull =
answer.answer(
invocationMethodWithArgs(
new HashSet<String>(Arrays.asList("set-1", "set-2"))));
assertThat(smartNull).isNotNull().isInstanceOf(Set.class);
assertThat((Set) smartNull).isEmpty();
}
@Test
public void
should_return_a_new_mock_that_has_been_defined_with_method_generic_and_provided_in_argument()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
final Foo mock = mock(Foo.class);
Object smartNull = answer.answer(invocationMethodWithArgs(mock));
assertThat(smartNull).isNotNull().isNotSameAs(mock);
assertThat(smartNull.toString())
.contains("SmartNull returned by")
.contains("genericFooBar.methodWithArgs(");
}
@Test
public void
should_return_an_Object_that_has_been_defined_with_method_generic_and_provided_in_argument()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
Object smartNull = answer.answer(invocationMethodWithArgs(new Object() {}));
assertThat(smartNull.toString())
.contains("SmartNull returned by")
.contains("genericFooBar.methodWithArgs(");
}
@Test
public void should_throw_a_error_on_invocation_of_returned_mock() throws Throwable {
final Answer<Object> answer = new ReturnsSmartNulls();
final Foo mock = mock(Foo.class);
final Throwable throwable =
Assertions.catchThrowable(
new ThrowableAssert.ThrowingCallable() {
@Override
public void call() throws Throwable {
((Foo) answer.answer(invocationMethodWithArgs(mock))).get();
}
});
Assertions.assertThat(throwable)
.isInstanceOf(SmartNullPointerException.class)
.hasMessageContaining("genericFooBar.methodWithArgs(")
.hasMessageContaining("1")
.hasMessageContaining(mock.toString());
}
private static <T> InterceptedInvocation invocationMethodWithVarArgs(final T[] obj)
throws NoSuchMethodException {
return new InterceptedInvocation(
new MockStrongReference<Object>(mock(GenericFooBar.class), false),
new SerializableMethod(
GenericFooBar.class.getMethod(
"methodWithVarArgs", int.class, Object[].class)),
new Object[] {1, obj},
InterceptedInvocation.NO_OP,
new LocationImpl(),
1);
}
@Test
public void
should_return_a_String_that_has_been_defined_with_method_generic_and_provided_in_var_args()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
Object smartNull =
answer.answer(invocationMethodWithVarArgs(new String[] {"varArg-1", "varArg-2"}));
assertThat(smartNull).isNotNull().isInstanceOf(String.class).asString().isEmpty();
}
@Test
public void
should_return_a_empty_list_that_has_been_defined_with_method_generic_and_provided_in_var_args()
throws Throwable {
final List<String> arg1 = Collections.singletonList("String");
final List<String> arg2 = Arrays.asList("str-1", "str-2");
Answer<Object> answer = new ReturnsSmartNulls();
Object smartNull = answer.answer(invocationMethodWithVarArgs(new List[] {arg1, arg2}));
assertThat(smartNull).isNotNull().isInstanceOf(List.class);
assertThat((List) smartNull).isEmpty();
}
@Test
public void
should_return_a_empty_map_that_has_been_defined_with_method_generic_and_provided_in_var_args()
throws Throwable {
final Map<String, String> map1 = new HashMap<>();
map1.put("key-1", "value-1");
map1.put("key-2", "value-2");
final Map<String, String> map2 = new HashMap<>();
map2.put("key-3", "value-1");
map2.put("key-4", "value-2");
Answer<Object> answer = new ReturnsSmartNulls();
Object smartNull = answer.answer(invocationMethodWithVarArgs(new Map[] {map1, map2}));
assertThat(smartNull).isNotNull().isInstanceOf(Map.class);
assertThat((Map) smartNull).isEmpty();
}
@Test
public void
should_return_a_empty_set_that_has_been_defined_with_method_generic_and_provided_in_var_args()
throws Throwable {
final HashSet<String> set1 = new HashSet<String>(Arrays.asList("set-1", "set-2"));
final HashSet<String> set2 = new HashSet<String>(Arrays.asList("set-1", "set-2"));
Answer<Object> answer = new ReturnsSmartNulls();
Object smartNull = answer.answer(invocationMethodWithVarArgs(new HashSet[] {set1, set2}));
assertThat(smartNull).isNotNull().isInstanceOf(Set.class);
assertThat((Set) smartNull).isEmpty();
}
@Test
public void
should_return_a_new_mock_that_has_been_defined_with_method_generic_and_provided_in_var_args()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
final Foo mock1 = mock(Foo.class);
final Foo mock2 = mock(Foo.class);
Object smartNull = answer.answer(invocationMethodWithVarArgs(new Foo[] {mock1, mock2}));
assertThat(smartNull).isNotNull().isNotSameAs(mock1).isNotSameAs(mock2);
assertThat(smartNull.toString())
.contains("SmartNull returned by")
.contains("genericFooBar.methodWithVarArgs(");
}
@Test
public void
should_return_an_Object_that_has_been_defined_with_method_generic_and_provided_in_var_args()
throws Throwable {
Answer<Object> answer = new ReturnsSmartNulls();
Object smartNull =
answer.answer(
invocationMethodWithVarArgs(
new Object[] {new Object() {}, new Object() {}}));
assertThat(smartNull.toString())
.contains("SmartNull returned by")
.contains("genericFooBar.methodWithVarArgs(");
}
}