-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
GenericTypeMockTest.java
376 lines (292 loc) · 11.3 KB
/
GenericTypeMockTest.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
/*
* Copyright (c) 2023 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockitousage;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.mockito.MockitoAnnotations.*;
import java.sql.Time;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
/**
* Tests that verify Mockito can discern mocks by generic types, so if there are multiple mock candidates
* with the same generic type but different type parameters available for injection into a given field,
* Mockito won't fail to inject (even if mock field name doesn't match under test's field name).
*/
@ExtendWith(MockitoExtension.class)
public class GenericTypeMockTest {
@Nested
public class SingleTypeParamTest {
public class UnderTestWithSingleTypeParam {
List<String> stringList;
List<Integer> intList;
}
@Mock
private List<String> stringListMock;
@Mock
private List<Integer> intListMock;
// must construct non-static inner class ourselves here
// (making it public static classes doesn't work either)
@InjectMocks
private UnderTestWithSingleTypeParam underTestWithSingleTypeParam = new UnderTestWithSingleTypeParam();
@Test
void testSingleTypeParam() {
// testing for not null first before testing for equals,
// because assertEquals(null, null) == true,
// so test would succeed if both @Mock and @InjectMocks
// don't work at all
assertNotNull(stringListMock);
assertNotNull(intListMock);
assertEquals(stringListMock, underTestWithSingleTypeParam.stringList);
assertEquals(intListMock, underTestWithSingleTypeParam.intList);
}
}
@Nested
public class WildcardTest {
class UnderTestWithWildcard {
Set<? extends Date> dateSet;
Set<? extends Number> numberSet;
}
@Mock
Set<Time> timeSetMock; // java.sql.Time extends Date
@Mock
Set<Integer> integerSetMock;
@InjectMocks
UnderTestWithWildcard underTestWithWildcard = new UnderTestWithWildcard();
@Test
void testWildcard() {
assertNotNull(timeSetMock);
assertNotNull(integerSetMock);
// this also tests whether WildcardType.upperBounds() is evaluated,
// i.e. that we match <? extends Date> to <Time> type parameter
assertEquals(timeSetMock, underTestWithWildcard.dateSet);
assertEquals(integerSetMock, underTestWithWildcard.numberSet);
}
}
@Nested
public class NestedTypeParametersTest {
public class UnderTestWithNestedTypeParameters {
Map<Integer, Collection<String>> intToStringCollectionMap;
Map<Integer, Collection<Integer>> intoToIntCollectionMap;
}
@Mock
Map<Integer, Collection<String>> intToStringCollectionMapMock;
@Mock
Map<Integer, Collection<Integer>> intToIntCollectionMapMock;
@InjectMocks
UnderTestWithNestedTypeParameters underTestWithNestedTypeParameters = new UnderTestWithNestedTypeParameters();
@Test
void testNestedTypeParameters() {
assertNotNull(intToStringCollectionMapMock);
assertNotNull(intToIntCollectionMapMock);
assertEquals(intToStringCollectionMapMock, underTestWithNestedTypeParameters.intToStringCollectionMap);
assertEquals(intToIntCollectionMapMock, underTestWithNestedTypeParameters.intoToIntCollectionMap);
}
}
@Nested
public class GenericSubclassTest {
public class UnderTestWithGenericSubclass {
Set<String> stringSet;
Set<Integer> intSet;
}
@Mock
TreeSet<String> stringTreeSetMock;
@Mock
HashSet<Integer> intHashSetMock;
@InjectMocks
UnderTestWithGenericSubclass underTestWithGenericSubclass = new UnderTestWithGenericSubclass();
@Test
void testGenericSubclass() {
assertNotNull(stringTreeSetMock);
assertNotNull(intHashSetMock);
assertEquals(stringTreeSetMock, underTestWithGenericSubclass.stringSet);
assertEquals(intHashSetMock, underTestWithGenericSubclass.intSet);
}
}
@Nested
public class MultipleCandidatesOneByNameTest {
public class UnderTestWithMultipleCandidatesOneByName {
List<String> stringList;
}
@Mock
List<String> stringList;
@Mock
List<String> stringListMock;
@InjectMocks
UnderTestWithMultipleCandidatesOneByName underTestWithMultipleCandidatesOneByName = new UnderTestWithMultipleCandidatesOneByName();
@Test
void testMultipleCandidatesOneByName() {
// verify that when multiple mock candidates exist by type, and one of them matches by field name, that one is injected
assertNotNull(underTestWithMultipleCandidatesOneByName.stringList);
assertEquals(stringList, underTestWithMultipleCandidatesOneByName.stringList);
}
}
@Nested
public class NoneMatchByTypeParameterTest {
public class UnderTestWithNoMatches {
List<Integer> intList;
}
@Mock
List<String> stringList;
@InjectMocks
UnderTestWithNoMatches underTestWithNoMatches = new UnderTestWithNoMatches();
@Test
void testNoneMatchByTypeParameter() {
assertNotNull(stringList);
// verify that when no candidate matches by type parameter, none is injected
assertNull(underTestWithNoMatches.intList);
}
}
@Nested
public class NoneMatchByRawTypeTest {
public class UnderTestWithNoMatches {
List<Integer> intList;
}
@Mock
Set<Integer> intSet;
@InjectMocks
UnderTestWithNoMatches underTestWithNoMatchesByRawType = new UnderTestWithNoMatches();
@Test
void testNoneMatchByRawType() {
assertNotNull(intSet);
// verify that when no candidate matches by raw type, none is injected
assertNull(underTestWithNoMatchesByRawType.intList);
}
}
@Nested
public class ClassWithTypeParameterNoMatchTest {
public class UnderTestWithTypeParameter<T> {
List<T> tList;
}
@Mock
List<Integer> intList;
@InjectMocks
UnderTestWithTypeParameter<String> underTestWithTypeParameterNoMatch = new UnderTestWithTypeParameter<String>();
@Test
void testWithTypeParameterNoMatch() {
assertNotNull(intList);
// verify that when no candidate matches by type parameter of class under test, none is injected
assertNull(underTestWithTypeParameterNoMatch.tList);
}
}
@Nested
public class ClassWithTypeParametersTest {
public class UnderTestWithTypeParameters<T1, T2> {
List<T1> t1List;
List<T2> t2List;
}
@Mock
List<String> stringList;
@Mock
List<Integer> intList;
@InjectMocks
UnderTestWithTypeParameters<String, Integer> underTestWithTypeParameters = new UnderTestWithTypeParameters<String, Integer>();
@Test
void testWithTypeParameters() {
assertNotNull(stringList);
assertNotNull(intList);
// verify that we can match the type parameters of the class under test
assertEquals(stringList, underTestWithTypeParameters.t1List);
assertEquals(intList, underTestWithTypeParameters.t2List);
}
}
@Nested
public class InjectConcreteClassInFieldWithTypeParameter {
public class UnderTestWithTypeParameter<T> {
List<T> tList;
}
public class ConcreteStringList extends ArrayList<String> {}
@Mock
ConcreteStringList concreteStringList;
@InjectMocks
UnderTestWithTypeParameter<String> underTestWithTypeParameters = new UnderTestWithTypeParameter<String>();
@Test
void testWithTypeParameters() {
assertNotNull(concreteStringList);
// verify that we can match the type parameters of the class under test
assertEquals(concreteStringList, underTestWithTypeParameters.tList);
}
}
@Nested
public class NoneMatchInjectConcreteClassInFieldWithTypeParameterTest {
public class UnderTestWithTypeParameter<T> {
List<T> tList;
}
public class ConcreteStringList extends ArrayList<String> {}
@Mock
ConcreteStringList concreteStringList;
@InjectMocks
UnderTestWithTypeParameter<Integer> underTestWithTypeParameters = new UnderTestWithTypeParameter<Integer>();
@Test
void testWithTypeParameters() {
assertNotNull(concreteStringList);
// verify that when no concrete type candidate matches, none is injected
assertNull(underTestWithTypeParameters.tList);
}
}
/**
* Verify regression https://github.com/mockito/mockito/issues/2958 is fixed.
*/
@Nested
public class RegressionClassCastException {
public class AbstractUnderTest<A extends AbstractUnderTest<A>> {
UnderTestInstance<A> instance;
}
public class UnderTestInstance<I extends AbstractUnderTest<I>> {
}
public class ConcreteUnderTest extends AbstractUnderTest<ConcreteUnderTest> {
}
@Mock
UnderTestInstance<ConcreteUnderTest> instanceMock;
@InjectMocks
protected ConcreteUnderTest concreteUnderTest = new ConcreteUnderTest();
@BeforeEach
public void initMocks()
{
openMocks(this);
}
@Test
public void testMockExists() {
assertNotNull(instanceMock);
assertEquals(instanceMock, concreteUnderTest.instance);
}
}
/**
* Verify regression https://github.com/mockito/mockito/issues/2974 is fixed.
*/
@Nested
public class RegressionNpe {
public abstract class Change {}
public class ChangeCollection<TChange extends Change> implements Iterable<TChange> {
private List<TChange> changes = new ArrayList<TChange>();
@Override
public Iterator<TChange> iterator() {
return null;
}
}
@Mock Change change0;
@InjectMocks ChangeCollection spiedImpl = new ChangeCollection();
@Mock(name = "changes") List<Change> innerList;
@Test
public void testNoNpe() {
assertSame(innerList, spiedImpl.changes);
}
}
}