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