View Javadoc
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   * The Class TestedClassWithFullDITest.
25   */
26  @TestMethodOrder(MethodName.class)
27  final class TestedClassWithFullDITest {
28  
29      /**
30       * The Class TestedClass.
31       */
32      public static class TestedClass {
33  
34          /** The dependency to be mocked. */
35          Runnable dependencyToBeMocked;
36  
37          /** The dependency 2. */
38          FirstLevelDependency dependency2;
39  
40          /** The dependency 3. */
41          FirstLevelDependency dependency3;
42  
43          /** The common dependency. */
44          CommonDependency commonDependency;
45  
46          /** The name. */
47          String name;
48  
49          /** The description. */
50          StringBuilder description;
51  
52          /** The number. */
53          final Integer number = null;
54  
55          /** The flag. */
56          boolean flag = true;
57  
58          /** The thread state. */
59          Thread.State threadState;
60  
61          /** The sub obj. */
62          AnotherTestedClass subObj;
63  
64          /** The sub obj 2. */
65          YetAnotherTestedClass subObj2;
66  
67          /** The not to be injected. */
68          volatile CommonDependency notToBeInjected;
69      }
70  
71      /**
72       * The Class FirstLevelDependency.
73       */
74      public static class FirstLevelDependency {
75  
76          /** The first level id. */
77          String firstLevelId;
78  
79          /** The dependency. */
80          SecondLevelDependency dependency;
81  
82          /** The common dependency. */
83          CommonDependency commonDependency;
84  
85          /** The dependency to be mocked. */
86          Runnable dependencyToBeMocked;
87      }
88  
89      /**
90       * The Class SecondLevelDependency.
91       */
92      public static class SecondLevelDependency {
93          /** The common dependency. */
94          CommonDependency commonDependency;
95      }
96  
97      /**
98       * The Class CommonDependency.
99       */
100     public static class CommonDependency {
101     }
102 
103     /**
104      * The Interface IntegrationTested.
105      */
106     @Tested(fullyInitialized = true)
107     @Retention(RetentionPolicy.RUNTIME)
108     @Target(ElementType.FIELD)
109     public @interface IntegrationTested {
110     }
111 
112     /**
113      * The Class YetAnotherTestedClass.
114      */
115     public static class YetAnotherTestedClass {
116     }
117 
118     /** The tested 3. */
119     @IntegrationTested
120     YetAnotherTestedClass tested3;
121 
122     /** The tested. */
123     @IntegrationTested
124     TestedClass tested;
125 
126     /** The mocked dependency. */
127     @Injectable
128     Runnable mockedDependency;
129 
130     /**
131      * Use fully initialized tested object with no injectable for first level dependency.
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      * Use fully initialized tested object with value for first level dependency.
142      *
143      * @param id
144      *            the id
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      * The Class AnotherTestedClass.
169      */
170     public static class AnotherTestedClass {
171         /** The sub obj. */
172         YetAnotherTestedClass subObj;
173     }
174 
175     /** The tested 2. */
176     @IntegrationTested
177     AnotherTestedClass tested2;
178 
179     /**
180      * Verify other tested objects get injected into first one.
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     /** The concrete dependency. */
190     @Tested
191     DependencyImpl concreteDependency;
192 
193     /** The tested 4. */
194     @IntegrationTested
195     ClassWithDependencyOfAbstractType tested4;
196 
197     /**
198      * The Interface Dependency.
199      */
200     public interface Dependency {
201     }
202 
203     /**
204      * The Class DependencyImpl.
205      */
206     static class DependencyImpl implements Dependency {
207     }
208 
209     /**
210      * The Class ClassWithDependencyOfAbstractType.
211      */
212     static class ClassWithDependencyOfAbstractType {
213         /** The dependency. */
214         Dependency dependency;
215     }
216 
217     /**
218      * Use tested object of subtype for abstract dependency type in another tested object.
219      */
220     @Test
221     void useTestedObjectOfSubtypeForAbstractDependencyTypeInAnotherTestedObject() {
222         assertSame(concreteDependency, tested4.dependency);
223     }
224 
225     /**
226      * The Class A.
227      */
228     static class A {
229         /** The b 1. */
230         B b1;
231     }
232 
233     /**
234      * The Class B.
235      */
236     static class B {
237         /** The b 2. */
238         B b2;
239     }
240 
241     /** The a. */
242     @Tested(fullyInitialized = true)
243     A a;
244 
245     /**
246      * Instantiate class dependent on another having field of its own type.
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      * The Class Person.
260      */
261     @Entity
262     static class Person {
263     }
264 
265     /**
266      * The Class ClassWithJPAEntityField.
267      */
268     static class ClassWithJPAEntityField {
269         /** The person. */
270         Person person;
271     }
272 
273     /**
274      * Instantiate class with JPA entity field.
275      *
276      * @param tested5
277      *            the tested 5
278      */
279     @Test
280     void instantiateClassWithJPAEntityField(@Tested(fullyInitialized = true) ClassWithJPAEntityField tested5) {
281         assertNull(tested5.person);
282     }
283 
284     /**
285      * The Class ClassWithDataSourceField.
286      */
287     static class ClassWithDataSourceField {
288         /** The ds. */
289         DataSource ds;
290     }
291 
292     /**
293      * Instantiate class with non annotated data source field.
294      *
295      * @param tested5
296      *            the tested 5
297      */
298     @Test
299     void instantiateClassWithNonAnnotatedDataSourceField(
300             @Tested(fullyInitialized = true) ClassWithDataSourceField tested5) {
301         assertNull(tested5.ds);
302     }
303 
304     /**
305      * The Class ClassWithJPAFields.
306      */
307     static class ClassWithJPAFields {
308         /** The em factory. */
309         EntityManagerFactory emFactory;
310         /** The em. */
311         EntityManager em;
312     }
313 
314     /**
315      * Instantiate class with non annotated JPA fields.
316      *
317      * @param tested6
318      *            the tested 6
319      */
320     @Test
321     void instantiateClassWithNonAnnotatedJPAFields(@Tested(fullyInitialized = true) ClassWithJPAFields tested6) {
322         // If an EntityManagerFactory was created for a previous test, then it got stored in the global dependency
323         // cache, which lasts
324         // until the end of the test run; therefore, the assertion needs to allow for that.
325         assertTrue(tested6.emFactory == null
326                 || tested6.emFactory.getClass().getName().contains("FakeEntityManagerFactory"));
327         assertNull(tested6.em);
328     }
329 
330     /**
331      * The Class ClassWithUnsatisfiableConstructor.
332      */
333     static class ClassWithUnsatisfiableConstructor {
334         /**
335          * Instantiates a new class with unsatisfiable constructor.
336          *
337          * @param someValue
338          *            the some value
339          */
340         ClassWithUnsatisfiableConstructor(@SuppressWarnings("unused") int someValue) {
341         }
342     }
343 
344     /**
345      * The Class ClassWithFieldToInject.
346      */
347     static class ClassWithFieldToInject {
348         /** The dependency. */
349         ClassWithUnsatisfiableConstructor dependency;
350     }
351 
352     /**
353      * Instantiate class with field to inject whose type cannot be instantiated.
354      *
355      * @param cut
356      *            the cut
357      */
358     @Test
359     void instantiateClassWithFieldToInjectWhoseTypeCannotBeInstantiated(
360             @Tested(fullyInitialized = true) ClassWithFieldToInject cut) {
361         assertNotNull(cut);
362         assertNull(cut.dependency);
363     }
364 
365     /**
366      * The Interface InterfaceDependency.
367      */
368     interface InterfaceDependency {
369     }
370 
371     /**
372      * The Class ClassWithInterfaceInConstructor.
373      */
374     static class ClassWithInterfaceInConstructor {
375         /**
376          * Instantiates a new class with interface in constructor.
377          *
378          * @param someValue
379          *            the some value
380          */
381         ClassWithInterfaceInConstructor(@SuppressWarnings("unused") InterfaceDependency someValue) {
382         }
383     }
384 
385     /**
386      * Instantiate class with interface in constructor.
387      *
388      * @param cut
389      *            the cut
390      */
391     @Test
392     void instantiateClassWithInterfaceInConstructor(
393             @Tested(fullyInitialized = true) ClassWithInterfaceInConstructor cut) {
394         assertNotNull(cut);
395     }
396 
397 }