1 package mockit;
2
3 import static java.util.Arrays.asList;
4
5 import static org.junit.jupiter.api.Assertions.assertEquals;
6 import static org.junit.jupiter.api.Assertions.assertNotNull;
7 import static org.junit.jupiter.api.Assertions.assertNull;
8 import static org.junit.jupiter.api.Assertions.assertSame;
9
10 import java.io.File;
11 import java.util.List;
12 import java.util.concurrent.Callable;
13
14 import javax.inject.Inject;
15 import javax.naming.InitialContext;
16 import javax.naming.NamingException;
17 import javax.sql.DataSource;
18
19 import org.junit.Before;
20 import org.junit.Test;
21
22
23
24
25 public final class TestedClassWithConstructorDI4Test {
26
27
28
29
30
31
32
33 static class GenericClass<T> {
34
35
36
37
38
39 T doSomething() {
40 return null;
41 }
42 }
43
44
45
46
47 @SuppressWarnings("FieldMayBeFinal")
48 public static final class TestedClass {
49
50
51 final GenericClass<String> go;
52
53
54 final List<Integer> values;
55
56
57 final Callable<Number> action1;
58
59
60 private Callable<Number> action2;
61
62
63 private Callable<Number> action3;
64
65
66 private DataSource database;
67
68
69
70
71
72
73
74
75
76
77
78 @SuppressWarnings("unchecked")
79 public TestedClass(GenericClass<String> go, List<Integer> values, Callable<Number>... actions) {
80 this.go = go;
81 this.values = values;
82 action1 = actions[0];
83 if (actions.length > 1) {
84 action2 = actions[1];
85 }
86 if (actions.length > 2) {
87 action3 = actions[2];
88 }
89
90 try {
91
92 InitialContext context = new InitialContext();
93 database = (DataSource) context.lookup("testDB");
94 context.close();
95 } catch (NamingException e) {
96 throw new RuntimeException(e);
97 }
98 }
99 }
100
101
102 @Tested
103 TestedClass tested;
104
105
106 @Injectable
107 Callable<Number> action1;
108
109
110 @Injectable
111 final GenericClass<String> mockGO = new GenericClass<>();
112
113 @Injectable
114 final List<Integer> numbers = asList(1, 2, 3);
115
116
117 @Mocked
118 InitialContext jndiContext;
119
120
121 @Mocked
122 DataSource testDB;
123
124
125
126
127
128
129
130 @Before
131 public void recordCommonExpectations() throws Exception {
132 new Expectations() {
133 {
134 mockGO.doSomething();
135 result = "test";
136 minTimes = 0;
137 }
138 };
139 new Expectations() {
140 {
141 jndiContext.lookup("testDB");
142 result = testDB;
143 }
144 };
145 }
146
147
148
149
150 @Test
151 public void exerciseTestedObjectWithValuesInjectedFromMockFields() {
152 assertNotNull(tested.go);
153 assertEquals(asList(1, 2, 3), tested.values);
154 assertSame(action1, tested.action1);
155 assertEquals("test", mockGO.doSomething());
156 assertNull(new GenericClass<String>().doSomething());
157 }
158
159
160
161
162
163
164
165
166
167 @Test
168 public void exerciseTestedObjectWithValuesInjectedFromMockParameters(@Injectable Callable<Number> action2,
169 @Injectable Callable<Number> action3) {
170 assertNotNull(tested.go);
171 assertEquals(asList(1, 2, 3), tested.values);
172 assertSame(action1, tested.action1);
173 assertSame(action2, tested.action2);
174 assertSame(action3, tested.action3);
175 assertEquals("test", mockGO.doSomething());
176 assertNull(new GenericClass().doSomething());
177 }
178
179
180
181
182
183
184
185 @Test
186 public void useMockedJREClassesDuringTestedObjectCreation(@Mocked File fileMock) {
187 assertNotNull(tested.database);
188 mockGO.doSomething();
189 }
190
191
192
193
194 static class TestedClass3 {
195
196
197 final String text;
198
199
200 final Runnable dependency;
201
202
203 @Inject
204 GenericClass<Integer> otherDep;
205
206
207
208
209
210
211
212
213
214 TestedClass3(String text, Runnable dependency) {
215 this.text = text;
216 this.dependency = dependency;
217 }
218 }
219
220
221 @Tested
222 TestedClass3 tested7;
223
224
225 @Injectable
226 final String text = null;
227
228
229 @Injectable
230 final Runnable dependency = null;
231
232
233 @Injectable
234 final GenericClass<Integer> otherDep = null;
235
236
237
238
239 @Test
240 public void injectNullsThroughConstructorParametersAndIntoRequiredField() {
241 assertNull(tested7.text);
242 assertNull(tested7.dependency);
243 assertNull(tested7.otherDep);
244 }
245 }