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
19
20 final class TestedClassWithConstructorAndFieldDI2Test {
21
22
23
24
25 public static final class TestedClass {
26
27
28 private final int i;
29
30
31 private final String name;
32
33
34 private final Runnable action1;
35
36
37 Runnable action2;
38
39
40 int i2;
41
42
43 String text;
44
45
46 String text2;
47
48
49 String text3;
50
51
52 List<String> names;
53
54
55
56
57
58
59
60
61
62
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
73
74 static final class TestedClass2 {
75
76 boolean flag;
77 }
78
79
80 @Tested
81 final TestedClass tested1 = new TestedClass(123, "test", null);
82
83
84 @Tested
85 TestedClass tested2;
86
87
88 @Tested
89 TestedClass2 tested3;
90
91
92 @Injectable
93 Runnable action;
94
95
96 @Injectable("-67")
97 int i2;
98
99
100 @Injectable
101 String text = "text";
102
103
104 @Injectable("8")
105 int intValue2;
106
107
108 @Injectable
109 final int intValue3 = 9;
110
111 @Injectable
112 final List<String> names = asList("Abc", "xyz");
113
114
115
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
129
130 @Test
131 void verifyTestedObjectsInjectedFromFieldsInTheTestClass() {
132 assertFieldsSetByTheConstructor();
133 assertFieldsSetThroughFieldInjectionFromInjectableFields();
134
135
136 assertNull(tested1.text2);
137 assertNull(tested2.text2);
138 }
139
140
141
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
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
172
173
174
175
176 @Test
177 void verifyTestedObjectsInjectedFromInjectableFieldsAndParameters(@Injectable("Test") String text2) {
178 assertFieldsSetByTheConstructor();
179
180
181 assertEquals("Test", tested1.text2);
182 assertEquals("Test", tested2.text2);
183
184
185 assertNull(tested1.text3);
186 assertNull(tested2.text3);
187 }
188
189
190
191
192
193
194
195
196
197
198
199 @Test
200 void verifyTestedObjectsInjectedFromParametersByName(@Injectable("two") String text2,
201 @Injectable("three") String text3, @Injectable("true") boolean flag) {
202 assertFieldsSetByTheConstructor();
203
204
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
214
215 static class ClassWithConstructorHavingReferenceTypeParameterAndDoubleSizedLocalVar {
216
217
218
219
220
221
222
223 @SuppressWarnings("unused")
224 ClassWithConstructorHavingReferenceTypeParameterAndDoubleSizedLocalVar(String s) {
225 long var = 1;
226 }
227 }
228
229
230 @Tested
231 ClassWithConstructorHavingReferenceTypeParameterAndDoubleSizedLocalVar sut;
232 }