1
2
3
4
5
6 package mockit;
7
8 import static java.lang.annotation.ElementType.PARAMETER;
9 import static java.lang.annotation.RetentionPolicy.RUNTIME;
10
11 import static org.junit.jupiter.api.Assertions.assertEquals;
12 import static org.junit.jupiter.api.Assertions.assertNotNull;
13 import static org.junit.jupiter.api.Assertions.assertNull;
14 import static org.junit.jupiter.api.Assertions.assertSame;
15
16 import jakarta.inject.Inject;
17
18 import java.lang.annotation.Retention;
19 import java.lang.annotation.Target;
20
21 import mockit.integration.junit5.JMockitExtension;
22
23 import org.junit.jupiter.api.Test;
24 import org.junit.jupiter.api.extension.ExtendWith;
25
26
27
28
29 @ExtendWith(JMockitExtension.class)
30 class TestedParametersTest {
31
32
33
34
35 static class TestedClass {
36
37
38 final int i;
39
40
41 final Collaborator collaborator;
42
43
44 Dependency dependency;
45
46
47
48
49 TestedClass() {
50 i = -1;
51 collaborator = null;
52 }
53
54
55
56
57
58
59
60
61
62 TestedClass(int i, Collaborator collaborator) {
63 this.i = i;
64 this.collaborator = collaborator;
65 }
66 }
67
68
69
70
71 static class Dependency {
72 }
73
74
75
76
77 static final class Collaborator {
78 }
79
80
81
82
83
84
85
86 @Test
87 void createTestedObjectForTestMethodParameter(@Tested Dependency dep) {
88 assertNotNull(dep);
89 }
90
91
92 @Tested
93 TestedClass tested1;
94
95
96 @Tested(fullyInitialized = true)
97 TestedClass tested2;
98
99
100
101
102
103
104
105 @Test
106 void injectTestedObjectFromTestMethodParameterIntoFullyInitializedTestedObject(@Tested Dependency dep) {
107 assertEquals(-1, tested2.i);
108 assertNull(tested2.collaborator);
109 assertSame(dep, tested2.dependency);
110 }
111
112
113
114
115
116
117
118
119
120 @Test
121 void injectTestedParametersIntoTestedFieldsUsingConstructor(@Tested("123") int i,
122 @Tested Collaborator collaborator) {
123 assertEquals(123, i);
124 assertNotNull(collaborator);
125
126 assertEquals(123, tested1.i);
127 assertSame(collaborator, tested1.collaborator);
128 assertNull(tested1.dependency);
129
130 assertEquals(123, tested2.i);
131 assertSame(collaborator, tested2.collaborator);
132 assertNotNull(tested2.dependency);
133 }
134
135
136
137
138 static class TestedClass2 {
139
140 CharSequence text;
141
142 Number n;
143
144 Comparable<Float> cmp;
145 }
146
147
148
149
150
151
152
153
154
155
156
157
158
159 @Test
160 void injectTestedParametersIntoTestedFieldsOfSupertypes(@Tested("test") String s, @Tested("123") Integer n,
161 @Tested("5.2") Float cmp, @Tested(fullyInitialized = true) TestedClass2 tested) {
162 assertEquals("test", tested.text);
163 assertEquals(123, tested.n.intValue());
164 assertEquals(5.2F, tested.cmp);
165 }
166
167
168
169
170 static class TestedClass3 {
171
172 String text;
173
174 Number number;
175 }
176
177
178
179
180
181
182
183
184
185
186
187 @Test
188 void injectTestedParametersWithValuesIntoFieldsOfRegularTestedObject(@Tested("test") String s,
189 @Tested("123") Integer n, @Tested TestedClass3 tested) {
190 assertEquals("test", tested.text);
191 assertEquals(123, tested.number);
192 }
193
194
195
196
197 static class TestedClass4 {
198
199
200 final String text;
201
202
203 final Number number;
204
205
206
207
208
209
210
211
212
213 TestedClass4(String text, Number number) {
214 this.text = text;
215 this.number = number;
216 }
217 }
218
219
220
221
222
223
224
225
226
227
228
229 @Test
230 void injectTestedParameterWithValueIntoRegularTestedObjectThroughConstructorParameter(@Tested("test") String text,
231 @Tested("1.23") Double number, @Tested TestedClass4 tested) {
232 assertEquals("test", tested.text);
233 assertEquals(1.23, tested.number);
234 }
235
236
237
238
239 static class AnotherDependency {
240 }
241
242
243
244
245 static class TestedClassWithDIAnnotatedField {
246
247 @Inject
248 AnotherDependency dep;
249 }
250
251
252 @Injectable
253 AnotherDependency anotherDep;
254
255
256
257
258
259
260
261 @Test
262 void injectInjectableFieldIntoTestedParameter(@Tested TestedClassWithDIAnnotatedField tested) {
263 assertSame(anotherDep, tested.dep);
264 }
265
266
267
268
269 @Target(PARAMETER)
270 @Retention(RUNTIME)
271 @Tested
272 public @interface InjectedDependency {
273 }
274
275
276
277
278
279
280
281 @Test
282 void injectParameterUsingTestedAsMetaAnnotation(@InjectedDependency Collaborator col) {
283 assertNotNull(col);
284 }
285 }