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 java.util.Arrays.asList;
9   
10  import static org.junit.jupiter.api.Assertions.assertEquals;
11  import static org.junit.jupiter.api.Assertions.assertNotNull;
12  import static org.junit.jupiter.api.Assertions.assertNotSame;
13  import static org.junit.jupiter.api.Assertions.assertNull;
14  import static org.junit.jupiter.api.Assertions.assertSame;
15  import static org.junit.jupiter.api.Assertions.assertTrue;
16  
17  import java.util.List;
18  
19  import mockit.integration.junit5.JMockitExtension;
20  
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.junit.jupiter.api.extension.ExtendWith;
24  
25  /**
26   * The Class TestedClassWithConstructorAndFieldDI2Test.
27   */
28  @ExtendWith(JMockitExtension.class)
29  class TestedClassWithConstructorAndFieldDI2Test {
30  
31      /**
32       * The Class TestedClass.
33       */
34      public static final class TestedClass {
35  
36          /** The i. */
37          private final int i;
38  
39          /** The name. */
40          private final String name;
41  
42          /** The action 1. */
43          private final Runnable action1;
44  
45          /** The action 2. */
46          Runnable action2;
47  
48          /** The i 2. */
49          int i2;
50  
51          /** The text. */
52          String text;
53  
54          /** The text 2. */
55          String text2;
56  
57          /** The text 3. */
58          String text3;
59  
60          /** The names. */
61          List<String> names;
62  
63          /**
64           * Instantiates a new tested class.
65           *
66           * @param i
67           *            the i
68           * @param name
69           *            the name
70           * @param action1
71           *            the action 1
72           */
73          public TestedClass(int i, String name, Runnable action1) {
74              this.i = i;
75              this.name = name;
76              this.action1 = action1;
77          }
78      }
79  
80      /**
81       * The Class TestedClass2.
82       */
83      static final class TestedClass2 {
84          /** The flag. */
85          boolean flag;
86      }
87  
88      /** The tested 1. */
89      @Tested
90      final TestedClass tested1 = new TestedClass(123, "test", null);
91  
92      /** The tested 2. */
93      @Tested
94      TestedClass tested2;
95  
96      /** The tested 3. */
97      @Tested
98      TestedClass2 tested3;
99  
100     /** The action. */
101     @Injectable
102     Runnable action;
103 
104     /** The i 2. */
105     @Injectable("-67")
106     int i2; // must match the target field by name
107 
108     /** The text. */
109     @Injectable
110     String text = "text";
111 
112     /** The int value 2. */
113     @Injectable("8")
114     int intValue2; // won't be used
115 
116     /** The int value 3. */
117     @Injectable
118     final int intValue3 = 9; // won't be used
119 
120     @Injectable
121     final List<String> names = asList("Abc", "xyz");
122 
123     /**
124      * Sets the up.
125      */
126     @BeforeEach
127     void setUp() {
128         Runnable action1 = new Runnable() {
129             @Override
130             public void run() {
131             }
132         };
133         tested2 = new TestedClass(45, "another", action1);
134     }
135 
136     /**
137      * Verify tested objects injected from fields in the test class.
138      */
139     @Test
140     void verifyTestedObjectsInjectedFromFieldsInTheTestClass() {
141         assertFieldsSetByTheConstructor();
142         assertFieldsSetThroughFieldInjectionFromInjectableFields();
143 
144         // Fields not set either way:
145         assertNull(tested1.text2);
146         assertNull(tested2.text2);
147     }
148 
149     /**
150      * Assert fields set by the constructor.
151      */
152     void assertFieldsSetByTheConstructor() {
153         assertEquals(123, tested1.i);
154         assertEquals("test", tested1.name);
155         assertNull(tested1.action1);
156 
157         assertEquals(45, tested2.i);
158         assertEquals("another", tested2.name);
159         assertNotNull(tested2.action1);
160         assertNotSame(action, tested2.action1);
161     }
162 
163     /**
164      * Assert fields set through field injection from injectable fields.
165      */
166     void assertFieldsSetThroughFieldInjectionFromInjectableFields() {
167         assertSame(action, tested1.action2);
168         assertEquals(-67, tested1.i2);
169         assertEquals("text", tested1.text);
170 
171         assertSame(action, tested2.action2);
172         assertEquals(-67, tested2.i2);
173         assertEquals("text", tested2.text);
174 
175         assertEquals(asList("Abc", "xyz"), tested1.names);
176         assertSame(tested1.names, tested2.names);
177     }
178 
179     /**
180      * Verify tested objects injected from injectable fields and parameters.
181      *
182      * @param text2
183      *            the text 2
184      */
185     @Test
186     void verifyTestedObjectsInjectedFromInjectableFieldsAndParameters(@Injectable("Test") String text2) {
187         assertFieldsSetByTheConstructor();
188 
189         // Fields set from injectable parameters:
190         assertEquals("Test", tested1.text2);
191         assertEquals("Test", tested2.text2);
192 
193         // Fields not set:
194         assertNull(tested1.text3);
195         assertNull(tested2.text3);
196     }
197 
198     /**
199      * Verify tested objects injected from parameters by name.
200      *
201      * @param text2
202      *            the text 2
203      * @param text3
204      *            the text 3
205      * @param flag
206      *            the flag
207      */
208     @Test
209     void verifyTestedObjectsInjectedFromParametersByName(@Injectable("two") String text2,
210             @Injectable("three") String text3, @Injectable("true") boolean flag) {
211         assertFieldsSetByTheConstructor();
212 
213         // Fields set from injectable parameters:
214         assertEquals("two", tested1.text2);
215         assertEquals("three", tested1.text3);
216         assertEquals("two", tested2.text2);
217         assertEquals("three", tested2.text3);
218         assertTrue(tested3.flag);
219     }
220 
221     /**
222      * The Class ClassWithConstructorHavingReferenceTypeParameterAndDoubleSizedLocalVar.
223      */
224     static class ClassWithConstructorHavingReferenceTypeParameterAndDoubleSizedLocalVar {
225 
226         /**
227          * Instantiates a new class with constructor having reference type parameter and double sized local var.
228          *
229          * @param s
230          *            the s
231          */
232         @SuppressWarnings("unused")
233         ClassWithConstructorHavingReferenceTypeParameterAndDoubleSizedLocalVar(String s) {
234             long var = 1;
235         }
236     }
237 
238     /** The sut. */
239     @Tested
240     ClassWithConstructorHavingReferenceTypeParameterAndDoubleSizedLocalVar sut;
241 }