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.assertSame;
6
7 import org.junit.jupiter.api.Test;
8
9
10
11
12 final class TestedClassWithFullConstructorAndFieldDITest {
13
14
15
16
17 static class TestedClass {
18
19
20 String value;
21
22
23 DependencyWithFieldDIOnly dependency1;
24
25
26 DependencyWithConstructorDIOnly dependency2;
27 }
28
29
30
31
32 static class DependencyWithFieldDIOnly {
33
34 String value;
35 }
36
37
38
39
40 static class DependencyWithConstructorDIOnly {
41
42
43 final String value;
44
45
46
47
48
49
50
51 DependencyWithConstructorDIOnly(String value) {
52 this.value = value;
53 }
54 }
55
56
57 @Tested(fullyInitialized = true)
58 TestedClass tested;
59
60
61 @Injectable
62 String first = "text";
63
64
65
66
67
68 @Test
69 void verifyEachTargetFieldGetsInjectedWithFirstUnusedInjectableWhetherThroughFieldOrConstructorInjection() {
70 assertEquals("text", tested.value);
71 assertEquals("text", tested.dependency1.value);
72 assertEquals("text", tested.dependency2.value);
73 }
74
75
76
77
78 @SuppressWarnings("unused")
79 static class ClassWithMultipleConstructors {
80
81
82 final int value;
83
84
85
86
87 ClassWithMultipleConstructors() {
88 value = 1;
89 }
90
91
92
93
94
95
96
97 ClassWithMultipleConstructors(int value) {
98 throw new RuntimeException("Not to be called");
99 }
100 }
101
102
103 @Tested(fullyInitialized = true)
104 ClassWithMultipleConstructors tested2;
105
106
107
108
109 @Test
110 void verifyInitializationOfClassWithMultipleConstructors() {
111 assertEquals(1, tested2.value);
112 }
113
114
115
116
117 static class ClassWithFieldToInject {
118
119 ClassWithMultipleConstructors dependency;
120 }
121
122
123 @Tested(fullyInitialized = true)
124 ClassWithFieldToInject tested3;
125
126
127
128
129 @Test
130 void verifyInitializationOfClassWithFieldOfAnotherClassHavingMultipleConstructors() {
131 assertNotNull(tested3.dependency);
132 assertEquals(1, tested3.dependency.value);
133 }
134
135
136
137
138 static final class Dependency {
139 }
140
141
142
143
144 @SuppressWarnings("unused")
145 static final class AnotherClassWithMultipleConstructors {
146
147
148 final Dependency dep;
149
150
151
152
153 AnotherClassWithMultipleConstructors() {
154 dep = new Dependency();
155 }
156
157
158
159
160
161
162
163 AnotherClassWithMultipleConstructors(Dependency dep) {
164 this.dep = dep;
165 }
166 }
167
168
169 @Tested
170 Dependency dep;
171
172
173 @Tested(fullyInitialized = true)
174 AnotherClassWithMultipleConstructors tested4;
175
176
177
178
179 @Test
180 void verifyInitializationOfClassWithMultipleConstructorsHavingTestedFieldForParameter() {
181 assertSame(dep, tested4.dep);
182 }
183
184
185
186
187 static class ClassWithFieldDI {
188
189 Dependency dep;
190 }
191
192
193
194
195 static class ClassWithConstructorDI {
196
197
198 ClassWithFieldDI dependency;
199
200
201
202
203
204
205
206 ClassWithConstructorDI(ClassWithFieldDI dependency) {
207 this.dependency = dependency;
208 }
209 }
210
211
212 @Tested(fullyInitialized = true)
213 ClassWithConstructorDI tested5;
214
215
216
217
218 @Test
219 void initializeClassWithConstructorInjectedDependencyHavingAnotherDependencyInjectedIntoField() {
220 assertNotNull(tested5.dependency);
221 assertNotNull(tested5.dependency.dep);
222 }
223 }