1
2
3
4
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
27
28 @ExtendWith(JMockitExtension.class)
29 class TestedClassWithConstructorAndFieldDI2Test {
30
31
32
33
34 public static final class TestedClass {
35
36
37 private final int i;
38
39
40 private final String name;
41
42
43 private final Runnable action1;
44
45
46 Runnable action2;
47
48
49 int i2;
50
51
52 String text;
53
54
55 String text2;
56
57
58 String text3;
59
60
61 List<String> names;
62
63
64
65
66
67
68
69
70
71
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
82
83 static final class TestedClass2 {
84
85 boolean flag;
86 }
87
88
89 @Tested
90 final TestedClass tested1 = new TestedClass(123, "test", null);
91
92
93 @Tested
94 TestedClass tested2;
95
96
97 @Tested
98 TestedClass2 tested3;
99
100
101 @Injectable
102 Runnable action;
103
104
105 @Injectable("-67")
106 int i2;
107
108
109 @Injectable
110 String text = "text";
111
112
113 @Injectable("8")
114 int intValue2;
115
116
117 @Injectable
118 final int intValue3 = 9;
119
120 @Injectable
121 final List<String> names = asList("Abc", "xyz");
122
123
124
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
138
139 @Test
140 void verifyTestedObjectsInjectedFromFieldsInTheTestClass() {
141 assertFieldsSetByTheConstructor();
142 assertFieldsSetThroughFieldInjectionFromInjectableFields();
143
144
145 assertNull(tested1.text2);
146 assertNull(tested2.text2);
147 }
148
149
150
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
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
181
182
183
184
185 @Test
186 void verifyTestedObjectsInjectedFromInjectableFieldsAndParameters(@Injectable("Test") String text2) {
187 assertFieldsSetByTheConstructor();
188
189
190 assertEquals("Test", tested1.text2);
191 assertEquals("Test", tested2.text2);
192
193
194 assertNull(tested1.text3);
195 assertNull(tested2.text3);
196 }
197
198
199
200
201
202
203
204
205
206
207
208 @Test
209 void verifyTestedObjectsInjectedFromParametersByName(@Injectable("two") String text2,
210 @Injectable("three") String text3, @Injectable("true") boolean flag) {
211 assertFieldsSetByTheConstructor();
212
213
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
223
224 static class ClassWithConstructorHavingReferenceTypeParameterAndDoubleSizedLocalVar {
225
226
227
228
229
230
231
232 @SuppressWarnings("unused")
233 ClassWithConstructorHavingReferenceTypeParameterAndDoubleSizedLocalVar(String s) {
234 long var = 1;
235 }
236 }
237
238
239 @Tested
240 ClassWithConstructorHavingReferenceTypeParameterAndDoubleSizedLocalVar sut;
241 }