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.assertArrayEquals;
9   import static org.junit.jupiter.api.Assertions.assertEquals;
10  import static org.junit.jupiter.api.Assertions.assertFalse;
11  import static org.junit.jupiter.api.Assertions.assertNotNull;
12  import static org.junit.jupiter.api.Assertions.assertTrue;
13  
14  import mockit.integration.junit5.JMockitExtension;
15  
16  import org.junit.jupiter.api.Test;
17  import org.junit.jupiter.api.extension.ExtendWith;
18  
19  /**
20   * The Class TestedClassWithConstructorDI0Test.
21   */
22  @ExtendWith(JMockitExtension.class)
23  @SuppressWarnings({ "UnusedParameters", "ClassWithTooManyFields" })
24  class TestedClassWithConstructorDI0Test {
25  
26      /**
27       * The Class TestedClassWithConstructorHavingPrimitiveParameter.
28       */
29      public static final class TestedClassWithConstructorHavingPrimitiveParameter {
30  
31          /**
32           * Instantiates a new tested class with constructor having primitive parameter.
33           *
34           * @param i
35           *            the i
36           */
37          public TestedClassWithConstructorHavingPrimitiveParameter(int i) {
38              assertEquals(123, i);
39          }
40      }
41  
42      /**
43       * The Class TestedClassWithConstructorHavingStringParameter.
44       */
45      public static final class TestedClassWithConstructorHavingStringParameter {
46  
47          /**
48           * Instantiates a new tested class with constructor having string parameter.
49           *
50           * @param s
51           *            the s
52           */
53          public TestedClassWithConstructorHavingStringParameter(String s) {
54          }
55      }
56  
57      /**
58       * The Class TestedClassWithConstructorHavingArrayParameter.
59       */
60      public static final class TestedClassWithConstructorHavingArrayParameter {
61  
62          /**
63           * Instantiates a new tested class with constructor having array parameter.
64           *
65           * @param arr
66           *            the arr
67           */
68          public TestedClassWithConstructorHavingArrayParameter(String[] arr) {
69              assertArrayEquals(new String[] { "abc", "Xyz" }, arr);
70          }
71      }
72  
73      /**
74       * The Class TestedClassWithConstructorHavingLongParameters.
75       */
76      public static final class TestedClassWithConstructorHavingLongParameters {
77  
78          /**
79           * Instantiates a new tested class with constructor having long parameters.
80           *
81           * @param l1
82           *            the l 1
83           * @param l2
84           *            the l 2
85           */
86          @SuppressWarnings("unused")
87          public TestedClassWithConstructorHavingLongParameters(long l1, long l2) {
88              assertEquals(1, l1);
89              assertEquals(2, l2);
90          }
91  
92          /**
93           * Instantiates a new tested class with constructor having long parameters.
94           *
95           * @param i
96           *            the i
97           * @param l1
98           *            the l 1
99           * @param l2
100          *            the l 2
101          */
102         @SuppressWarnings("unused")
103         TestedClassWithConstructorHavingLongParameters(int i, long l1, long l2) {
104             throw new RuntimeException("Must not occur");
105         }
106     }
107 
108     /**
109      * The Class TestedClassWithConstructorHavingDoubleParameters.
110      */
111     public static final class TestedClassWithConstructorHavingDoubleParameters {
112 
113         /**
114          * Instantiates a new tested class with constructor having double parameters.
115          *
116          * @param d1
117          *            the d 1
118          * @param d2
119          *            the d 2
120          */
121         TestedClassWithConstructorHavingDoubleParameters(double d1, double d2) {
122             assertEquals(1.0, d1, 0);
123             assertEquals(2.0, d2, 0);
124         }
125     }
126 
127     /**
128      * The Class TestedClassWithConstructorHavingVarargsParameter.
129      */
130     public static final class TestedClassWithConstructorHavingVarargsParameter {
131 
132         /**
133          * Instantiates a new tested class with constructor having varargs parameter.
134          *
135          * @param b
136          *            the b
137          * @param c
138          *            the c
139          * @param s
140          *            the s
141          * @param b2
142          *            the b 2
143          * @param flags
144          *            the flags
145          */
146         public TestedClassWithConstructorHavingVarargsParameter(byte b, char c, String s, byte b2, boolean... flags) {
147             assertEquals(56, b);
148             assertEquals(57, b2);
149             assertEquals('X', c);
150             assertEquals("test", s);
151             assertEquals(3, flags.length);
152             assertTrue(flags[0]);
153             assertFalse(flags[1]);
154             assertTrue(flags[2]);
155         }
156     }
157 
158     /**
159      * The Class TestedClassWithConstructorHavingDoubleSizeParameterFollowedByRegularParameters.
160      */
161     public static final class TestedClassWithConstructorHavingDoubleSizeParameterFollowedByRegularParameters {
162 
163         /**
164          * Instantiates a new tested class with constructor having double size parameter followed by regular parameters.
165          *
166          * @param l1
167          *            the l 1
168          * @param c
169          *            the c
170          * @param s
171          *            the s
172          */
173         public TestedClassWithConstructorHavingDoubleSizeParameterFollowedByRegularParameters(long l1, char c,
174                 String s) {
175             assertEquals(1, l1);
176             assertEquals('X', c);
177             assertEquals("test", s);
178         }
179     }
180 
181     /** The tested 0. */
182     @Tested
183     TestedClassWithConstructorHavingPrimitiveParameter tested0;
184 
185     /** The tested 1. */
186     @Tested
187     TestedClassWithConstructorHavingStringParameter tested1;
188 
189     /** The tested 2. */
190     @Tested
191     TestedClassWithConstructorHavingArrayParameter tested2;
192 
193     /** The tested 3. */
194     @Tested
195     TestedClassWithConstructorHavingLongParameters tested3;
196 
197     /** The tested 4. */
198     @Tested
199     TestedClassWithConstructorHavingDoubleParameters tested4;
200 
201     /** The tested 5. */
202     @Tested
203     TestedClassWithConstructorHavingVarargsParameter tested5;
204 
205     /** The tested 6. */
206     @Tested
207     TestedClassWithConstructorHavingDoubleSizeParameterFollowedByRegularParameters tested6;
208 
209     /** The i. */
210     @Injectable
211     int i = 123;
212 
213     /** The unused. */
214     @Injectable
215     int unused;
216 
217     /** The l 1. */
218     @Injectable
219     long l1 = 1;
220 
221     /** The l 2. */
222     @Injectable
223     final long l2 = 2;
224 
225     /** The arr. */
226     @Injectable
227     String[] arr = { "abc", "Xyz" };
228 
229     /** The b. */
230     @Injectable
231     byte b = 56;
232 
233     /** The b 2. */
234     @Injectable
235     byte b2 = 57;
236 
237     /** The c. */
238     @Injectable
239     char c = 'X';
240 
241     /** The s. */
242     @Injectable
243     String s = "test";
244 
245     /** The d 1. */
246     @Injectable
247     double d1 = 1.0;
248 
249     /** The d 2. */
250     @Injectable
251     double d2 = 2.0;
252 
253     /** The first flag. */
254     // For varargs parameter:
255     @Injectable
256     boolean firstFlag = true;
257 
258     /** The second flag. */
259     @Injectable("false")
260     boolean secondFlag;
261 
262     /** The third flag. */
263     @Injectable
264     boolean thirdFlag = true;
265 
266     /**
267      * Verify instantiation of tested objects through constructors with non mocked parameters.
268      */
269     @Test
270     void verifyInstantiationOfTestedObjectsThroughConstructorsWithNonMockedParameters() {
271         assertNotNull(tested0);
272         assertNotNull(tested1);
273         assertNotNull(tested2);
274         assertNotNull(tested3);
275         assertNotNull(tested4);
276         assertNotNull(tested5);
277         assertNotNull(tested6);
278     }
279 }