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