1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertNotNull;
5 import static org.junit.jupiter.api.Assertions.assertNull;
6 import static org.junit.jupiter.api.Assertions.assertSame;
7 import static org.junit.jupiter.api.Assertions.assertTrue;
8
9 import java.lang.annotation.ElementType;
10 import java.lang.annotation.Retention;
11 import java.lang.annotation.RetentionPolicy;
12 import java.lang.annotation.Target;
13
14 import javax.persistence.Entity;
15 import javax.persistence.EntityManager;
16 import javax.persistence.EntityManagerFactory;
17 import javax.sql.DataSource;
18
19 import org.junit.jupiter.api.MethodOrderer.MethodName;
20 import org.junit.jupiter.api.Test;
21 import org.junit.jupiter.api.TestMethodOrder;
22
23
24
25
26 @TestMethodOrder(MethodName.class)
27 final class TestedClassWithFullDITest {
28
29
30
31
32 public static class TestedClass {
33
34
35 Runnable dependencyToBeMocked;
36
37
38 FirstLevelDependency dependency2;
39
40
41 FirstLevelDependency dependency3;
42
43
44 CommonDependency commonDependency;
45
46
47 String name;
48
49
50 StringBuilder description;
51
52
53 final Integer number = null;
54
55
56 boolean flag = true;
57
58
59 Thread.State threadState;
60
61
62 AnotherTestedClass subObj;
63
64
65 YetAnotherTestedClass subObj2;
66
67
68 volatile CommonDependency notToBeInjected;
69 }
70
71
72
73
74 public static class FirstLevelDependency {
75
76
77 String firstLevelId;
78
79
80 SecondLevelDependency dependency;
81
82
83 CommonDependency commonDependency;
84
85
86 Runnable dependencyToBeMocked;
87 }
88
89
90
91
92 public static class SecondLevelDependency {
93
94 CommonDependency commonDependency;
95 }
96
97
98
99
100 public static class CommonDependency {
101 }
102
103
104
105
106 @Tested(fullyInitialized = true)
107 @Retention(RetentionPolicy.RUNTIME)
108 @Target(ElementType.FIELD)
109 public @interface IntegrationTested {
110 }
111
112
113
114
115 public static class YetAnotherTestedClass {
116 }
117
118
119 @IntegrationTested
120 YetAnotherTestedClass tested3;
121
122
123 @IntegrationTested
124 TestedClass tested;
125
126
127 @Injectable
128 Runnable mockedDependency;
129
130
131
132
133 @Test
134 void useFullyInitializedTestedObjectWithNoInjectableForFirstLevelDependency() {
135 assertNull(tested.name);
136 assertSame(tested.commonDependency, tested.dependency2.dependency.commonDependency);
137 assertNull(tested.notToBeInjected);
138 }
139
140
141
142
143
144
145
146 @Test
147 void useFullyInitializedTestedObjectWithValueForFirstLevelDependency(@Injectable("test") String id) {
148 assertEquals("test", tested.name);
149 assertNull(tested.description);
150 assertNull(tested.number);
151 assertTrue(tested.flag);
152 assertNull(tested.threadState);
153 assertSame(mockedDependency, tested.dependencyToBeMocked);
154 assertNotNull(tested.dependency2);
155 assertEquals("test", tested.dependency2.firstLevelId);
156 assertSame(tested.dependency2, tested.dependency3);
157 assertNotNull(tested.commonDependency);
158 assertNotNull(tested.dependency2.dependency);
159 assertSame(tested.dependency2.dependency, tested.dependency3.dependency);
160 assertSame(tested.commonDependency, tested.dependency2.commonDependency);
161 assertSame(tested.commonDependency, tested.dependency3.commonDependency);
162 assertSame(tested.commonDependency, tested.dependency2.dependency.commonDependency);
163 assertSame(mockedDependency, tested.dependency2.dependencyToBeMocked);
164 assertSame(mockedDependency, tested.dependency3.dependencyToBeMocked);
165 }
166
167
168
169
170 public static class AnotherTestedClass {
171
172 YetAnotherTestedClass subObj;
173 }
174
175
176 @IntegrationTested
177 AnotherTestedClass tested2;
178
179
180
181
182 @Test
183 void verifyOtherTestedObjectsGetInjectedIntoFirstOne() {
184 assertSame(tested2, tested.subObj);
185 assertSame(tested3, tested.subObj2);
186 assertSame(tested3, tested.subObj.subObj);
187 }
188
189
190 @Tested
191 DependencyImpl concreteDependency;
192
193
194 @IntegrationTested
195 ClassWithDependencyOfAbstractType tested4;
196
197
198
199
200 public interface Dependency {
201 }
202
203
204
205
206 static class DependencyImpl implements Dependency {
207 }
208
209
210
211
212 static class ClassWithDependencyOfAbstractType {
213
214 Dependency dependency;
215 }
216
217
218
219
220 @Test
221 void useTestedObjectOfSubtypeForAbstractDependencyTypeInAnotherTestedObject() {
222 assertSame(concreteDependency, tested4.dependency);
223 }
224
225
226
227
228 static class A {
229
230 B b1;
231 }
232
233
234
235
236 static class B {
237
238 B b2;
239 }
240
241
242 @Tested(fullyInitialized = true)
243 A a;
244
245
246
247
248 @Test
249 void instantiateClassDependentOnAnotherHavingFieldOfItsOwnType() {
250 B b1 = a.b1;
251 assertNotNull(b1);
252
253 B b2 = b1.b2;
254 assertNotNull(b2);
255 assertSame(b1, b2);
256 }
257
258
259
260
261 @Entity
262 static class Person {
263 }
264
265
266
267
268 static class ClassWithJPAEntityField {
269
270 Person person;
271 }
272
273
274
275
276
277
278
279 @Test
280 void instantiateClassWithJPAEntityField(@Tested(fullyInitialized = true) ClassWithJPAEntityField tested5) {
281 assertNull(tested5.person);
282 }
283
284
285
286
287 static class ClassWithDataSourceField {
288
289 DataSource ds;
290 }
291
292
293
294
295
296
297
298 @Test
299 void instantiateClassWithNonAnnotatedDataSourceField(
300 @Tested(fullyInitialized = true) ClassWithDataSourceField tested5) {
301 assertNull(tested5.ds);
302 }
303
304
305
306
307 static class ClassWithJPAFields {
308
309 EntityManagerFactory emFactory;
310
311 EntityManager em;
312 }
313
314
315
316
317
318
319
320 @Test
321 void instantiateClassWithNonAnnotatedJPAFields(@Tested(fullyInitialized = true) ClassWithJPAFields tested6) {
322
323
324
325 assertTrue(tested6.emFactory == null
326 || tested6.emFactory.getClass().getName().contains("FakeEntityManagerFactory"));
327 assertNull(tested6.em);
328 }
329
330
331
332
333 static class ClassWithUnsatisfiableConstructor {
334
335
336
337
338
339
340 ClassWithUnsatisfiableConstructor(@SuppressWarnings("unused") int someValue) {
341 }
342 }
343
344
345
346
347 static class ClassWithFieldToInject {
348
349 ClassWithUnsatisfiableConstructor dependency;
350 }
351
352
353
354
355
356
357
358 @Test
359 void instantiateClassWithFieldToInjectWhoseTypeCannotBeInstantiated(
360 @Tested(fullyInitialized = true) ClassWithFieldToInject cut) {
361 assertNotNull(cut);
362 assertNull(cut.dependency);
363 }
364
365
366
367
368 interface InterfaceDependency {
369 }
370
371
372
373
374 static class ClassWithInterfaceInConstructor {
375
376
377
378
379
380
381 ClassWithInterfaceInConstructor(@SuppressWarnings("unused") InterfaceDependency someValue) {
382 }
383 }
384
385
386
387
388
389
390
391 @Test
392 void instantiateClassWithInterfaceInConstructor(
393 @Tested(fullyInitialized = true) ClassWithInterfaceInConstructor cut) {
394 assertNotNull(cut);
395 }
396
397 }