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