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