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.assertNotSame;
6   import static org.junit.jupiter.api.Assertions.assertSame;
7   import static org.junit.jupiter.api.Assertions.assertTrue;
8   
9   import javax.inject.Inject;
10  import javax.inject.Named;
11  
12  import org.junit.jupiter.api.Test;
13  import org.springframework.beans.factory.annotation.Qualifier;
14  
15  /**
16   * The Class TestedClassWithFullConstructorDITest.
17   */
18  final class TestedClassWithFullConstructorDITest {
19  
20      /**
21       * The Interface Dependency.
22       */
23      public interface Dependency {
24      }
25  
26      /**
27       * The Class DependencyImpl.
28       */
29      public static final class DependencyImpl implements Dependency {
30      }
31  
32      /**
33       * The Class Collaborator.
34       */
35      public static class Collaborator {
36      }
37  
38      /**
39       * The Class TestedClassWithSinglePublicConstructor.
40       */
41      @SuppressWarnings("unused")
42      public static final class TestedClassWithSinglePublicConstructor {
43  
44          /** The dependency. */
45          final Dependency dependency;
46  
47          /** The collaborator 1. */
48          final Collaborator collaborator1;
49  
50          /** The collaborator 2. */
51          Collaborator collaborator2;
52  
53          /**
54           * Instantiates a new tested class with single public constructor.
55           *
56           * @param dependency
57           *            the dependency
58           * @param collaborator
59           *            the collaborator
60           */
61          public TestedClassWithSinglePublicConstructor(Dependency dependency, Collaborator collaborator) {
62              this.dependency = dependency;
63              collaborator1 = collaborator;
64          }
65  
66          /**
67           * Instantiates a new tested class with single public constructor.
68           */
69          TestedClassWithSinglePublicConstructor() {
70              dependency = null;
71              collaborator1 = null;
72          }
73      }
74  
75      /** The dep. */
76      @Tested
77      DependencyImpl dep;
78  
79      /** The tested 1. */
80      @Tested(fullyInitialized = true)
81      TestedClassWithSinglePublicConstructor tested1;
82  
83      /**
84       * Verify instantiation of tested objects through public constructor.
85       */
86      @Test
87      void verifyInstantiationOfTestedObjectsThroughPublicConstructor() {
88          assertTrue(tested1.dependency instanceof DependencyImpl);
89          assertNotNull(tested1.collaborator1);
90          assertSame(tested1.collaborator1, tested1.collaborator2);
91      }
92  
93      /**
94       * The Class TestedClassWithAnnotatedConstructor.
95       */
96      public static final class TestedClassWithAnnotatedConstructor {
97  
98          /** The i. */
99          int i;
100 
101         /** The s. */
102         String s;
103 
104         /** The dependency. */
105         Dependency dependency;
106 
107         /** The collaborator 1. */
108         Collaborator collaborator1;
109 
110         /** The collaborator 2. */
111         @Inject
112         Collaborator collaborator2;
113 
114         /**
115          * Instantiates a new tested class with annotated constructor.
116          */
117         @SuppressWarnings("unused")
118         public TestedClassWithAnnotatedConstructor() {
119         }
120 
121         /**
122          * Instantiates a new tested class with annotated constructor.
123          *
124          * @param i
125          *            the i
126          * @param s
127          *            the s
128          * @param dependency
129          *            the dependency
130          * @param collaborator
131          *            the collaborator
132          */
133         @Inject
134         TestedClassWithAnnotatedConstructor(int i, String s, Dependency dependency, Collaborator collaborator) {
135             this.i = i;
136             this.s = s;
137             this.dependency = dependency;
138             collaborator1 = collaborator;
139         }
140     }
141 
142     /** The tested 2. */
143     @Tested(fullyInitialized = true)
144     TestedClassWithAnnotatedConstructor tested2;
145 
146     /** The number. */
147     @Injectable
148     final int number = 123;
149 
150     /** The text. */
151     @Injectable
152     final String text = "text";
153 
154     /**
155      * Verify instantiation of tested object through annotated constructor.
156      */
157     @Test
158     void verifyInstantiationOfTestedObjectThroughAnnotatedConstructor() {
159         assertNotNull(tested2);
160         assertEquals(123, tested2.i);
161         assertEquals("text", tested2.s);
162         assertTrue(tested2.dependency instanceof DependencyImpl);
163         assertNotNull(tested2.collaborator1);
164         assertSame(tested2.collaborator1, tested2.collaborator2);
165     }
166 
167     /**
168      * The Class TestedClassWithQualifiedConstructorParameters.
169      */
170     static class TestedClassWithQualifiedConstructorParameters {
171 
172         /** The col 1. */
173         final Collaborator col1;
174 
175         /** The col 2. */
176         final Collaborator col2;
177 
178         /**
179          * Instantiates a new tested class with qualified constructor parameters.
180          *
181          * @param p1
182          *            the p 1
183          * @param p2
184          *            the p 2
185          */
186         TestedClassWithQualifiedConstructorParameters(@Named("one") Collaborator p1,
187                 @Qualifier("two") Collaborator p2) {
188             col1 = p1;
189             col2 = p2;
190         }
191     }
192 
193     /** The tested 3. */
194     @Tested(fullyInitialized = true)
195     TestedClassWithQualifiedConstructorParameters tested3;
196 
197     /**
198      * Verify instantiation of tested class with qualified constructor parameters.
199      */
200     @Test
201     void verifyInstantiationOfTestedClassWithQualifiedConstructorParameters() {
202         assertNotNull(tested3.col1);
203         assertNotSame(tested3.col1, tested3.col2);
204     }
205 
206     /**
207      * The Class TestedClassWithDependencyHavingConstructorParameter.
208      */
209     static class TestedClassWithDependencyHavingConstructorParameter {
210 
211         /** The dependency. */
212         final DependencyWithConstructorParameter dependency;
213 
214         /**
215          * Instantiates a new tested class with dependency having constructor parameter.
216          *
217          * @param dependency
218          *            the dependency
219          */
220         TestedClassWithDependencyHavingConstructorParameter(DependencyWithConstructorParameter dependency) {
221             this.dependency = dependency;
222         }
223     }
224 
225     /**
226      * The Class DependencyWithConstructorParameter.
227      */
228     static class DependencyWithConstructorParameter {
229 
230         /** The par. */
231         final String par;
232 
233         /**
234          * Instantiates a new dependency with constructor parameter.
235          *
236          * @param par1
237          *            the par 1
238          */
239         DependencyWithConstructorParameter(String par1) {
240             par = par1;
241         }
242     }
243 
244     /** The tested 4. */
245     @Tested(fullyInitialized = true)
246     TestedClassWithDependencyHavingConstructorParameter tested4;
247 
248     /**
249      * Verify recursive instantiation of dependency with constructor parameter.
250      */
251     @Test
252     void verifyRecursiveInstantiationOfDependencyWithConstructorParameter() {
253         assertEquals("text", tested4.dependency.par);
254     }
255 }