1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertNotNull;
5 import static org.junit.jupiter.api.Assertions.assertNotSame;
6 import static org.junit.jupiter.api.Assertions.assertSame;
7 import static org.junit.jupiter.api.Assertions.assertTrue;
8
9 import javax.inject.Inject;
10 import javax.inject.Named;
11
12 import org.junit.jupiter.api.Test;
13 import org.springframework.beans.factory.annotation.Qualifier;
14
15
16
17
18 final class TestedClassWithFullConstructorDITest {
19
20
21
22
23 public interface Dependency {
24 }
25
26
27
28
29 public static final class DependencyImpl implements Dependency {
30 }
31
32
33
34
35 public static class Collaborator {
36 }
37
38
39
40
41 @SuppressWarnings("unused")
42 public static final class TestedClassWithSinglePublicConstructor {
43
44
45 final Dependency dependency;
46
47
48 final Collaborator collaborator1;
49
50
51 Collaborator collaborator2;
52
53
54
55
56
57
58
59
60
61 public TestedClassWithSinglePublicConstructor(Dependency dependency, Collaborator collaborator) {
62 this.dependency = dependency;
63 collaborator1 = collaborator;
64 }
65
66
67
68
69 TestedClassWithSinglePublicConstructor() {
70 dependency = null;
71 collaborator1 = null;
72 }
73 }
74
75
76 @Tested
77 DependencyImpl dep;
78
79
80 @Tested(fullyInitialized = true)
81 TestedClassWithSinglePublicConstructor tested1;
82
83
84
85
86 @Test
87 void verifyInstantiationOfTestedObjectsThroughPublicConstructor() {
88 assertTrue(tested1.dependency instanceof DependencyImpl);
89 assertNotNull(tested1.collaborator1);
90 assertSame(tested1.collaborator1, tested1.collaborator2);
91 }
92
93
94
95
96 public static final class TestedClassWithAnnotatedConstructor {
97
98
99 int i;
100
101
102 String s;
103
104
105 Dependency dependency;
106
107
108 Collaborator collaborator1;
109
110
111 @Inject
112 Collaborator collaborator2;
113
114
115
116
117 @SuppressWarnings("unused")
118 public TestedClassWithAnnotatedConstructor() {
119 }
120
121
122
123
124
125
126
127
128
129
130
131
132
133 @Inject
134 TestedClassWithAnnotatedConstructor(int i, String s, Dependency dependency, Collaborator collaborator) {
135 this.i = i;
136 this.s = s;
137 this.dependency = dependency;
138 collaborator1 = collaborator;
139 }
140 }
141
142
143 @Tested(fullyInitialized = true)
144 TestedClassWithAnnotatedConstructor tested2;
145
146
147 @Injectable
148 final int number = 123;
149
150
151 @Injectable
152 final String text = "text";
153
154
155
156
157 @Test
158 void verifyInstantiationOfTestedObjectThroughAnnotatedConstructor() {
159 assertNotNull(tested2);
160 assertEquals(123, tested2.i);
161 assertEquals("text", tested2.s);
162 assertTrue(tested2.dependency instanceof DependencyImpl);
163 assertNotNull(tested2.collaborator1);
164 assertSame(tested2.collaborator1, tested2.collaborator2);
165 }
166
167
168
169
170 static class TestedClassWithQualifiedConstructorParameters {
171
172
173 final Collaborator col1;
174
175
176 final Collaborator col2;
177
178
179
180
181
182
183
184
185
186 TestedClassWithQualifiedConstructorParameters(@Named("one") Collaborator p1,
187 @Qualifier("two") Collaborator p2) {
188 col1 = p1;
189 col2 = p2;
190 }
191 }
192
193
194 @Tested(fullyInitialized = true)
195 TestedClassWithQualifiedConstructorParameters tested3;
196
197
198
199
200 @Test
201 void verifyInstantiationOfTestedClassWithQualifiedConstructorParameters() {
202 assertNotNull(tested3.col1);
203 assertNotSame(tested3.col1, tested3.col2);
204 }
205
206
207
208
209 static class TestedClassWithDependencyHavingConstructorParameter {
210
211
212 final DependencyWithConstructorParameter dependency;
213
214
215
216
217
218
219
220 TestedClassWithDependencyHavingConstructorParameter(DependencyWithConstructorParameter dependency) {
221 this.dependency = dependency;
222 }
223 }
224
225
226
227
228 static class DependencyWithConstructorParameter {
229
230
231 final String par;
232
233
234
235
236
237
238
239 DependencyWithConstructorParameter(String par1) {
240 par = par1;
241 }
242 }
243
244
245 @Tested(fullyInitialized = true)
246 TestedClassWithDependencyHavingConstructorParameter tested4;
247
248
249
250
251 @Test
252 void verifyRecursiveInstantiationOfDependencyWithConstructorParameter() {
253 assertEquals("text", tested4.dependency.par);
254 }
255 }