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