1
2
3
4
5
6 package mockit;
7
8 import static org.junit.jupiter.api.Assertions.assertFalse;
9 import static org.junit.jupiter.api.Assertions.assertTrue;
10
11 import java.util.Observable;
12 import java.util.Observer;
13 import java.util.concurrent.Callable;
14
15 import mockit.integration.junit5.JMockitExtension;
16
17 import org.junit.jupiter.api.Test;
18 import org.junit.jupiter.api.extension.ExtendWith;
19
20
21
22
23 @ExtendWith(JMockitExtension.class)
24 class MultipleMockedTypesTest {
25
26
27
28
29 public static class FirstDependency {
30
31
32
33
34
35 public int getValue() {
36 return 1;
37 }
38 }
39
40
41
42
43 public static class SecondDependency {
44
45
46
47
48
49
50 public int getValue() {
51 return 2;
52 }
53
54
55
56
57
58
59 public int getDifferentValue() {
60 return 3;
61 }
62 }
63
64
65
66
67 public static class TestedUnit {
68
69
70
71
72
73
74
75
76
77 public boolean validate(FirstDependency first) {
78 SecondDependency second = new SecondDependency();
79 return first.getValue() + second.getValue() <= 0;
80 }
81
82
83
84
85
86
87
88
89
90 public boolean validateWithDifferentValue(FirstDependency first) {
91 SecondDependency second = new SecondDependency();
92 return first.getValue() + second.getDifferentValue() <= 0;
93 }
94
95
96
97
98
99
100
101
102
103
104
105 public boolean validate(FirstDependency first, SecondDependency second) {
106 return first.getValue() + second.getValue() <= 0;
107 }
108
109
110
111
112 static void doSomethingWithInternallyCreatedImplementations() {
113 new Observer() {
114 @Override
115 public void update(Observable o, Object arg) {
116 throw new IllegalStateException();
117 }
118 }.update(null, "event");
119
120 new Callable<String>() {
121 @Override
122 public String call() {
123 return "tested";
124 }
125 }.call();
126 }
127 }
128
129
130 @Mocked
131 FirstDependency mock1;
132
133
134
135
136
137
138
139 @Test
140 void invocationsOnMethodsOfDifferentClassesWithDifferentSignatures(@Mocked final SecondDependency mock2) {
141 new Expectations() {
142 {
143 mock1.getValue();
144 result = 15;
145 mock2.getDifferentValue();
146 result = -50;
147 }
148 };
149
150 assertTrue(new TestedUnit().validateWithDifferentValue(mock1));
151 }
152
153
154
155
156
157
158
159 @Test
160 void invocationsOnMethodsOfDifferentClassesButSameSignature(@Mocked final SecondDependency mock2) {
161 new Expectations() {
162 {
163 mock1.getValue();
164 result = 15;
165 mock2.getValue();
166 result = -50;
167 }
168 };
169
170 assertTrue(new TestedUnit().validate(mock1));
171
172 new VerificationsInOrder() {
173 {
174 mock1.getValue();
175 mock2.getValue();
176 }
177 };
178 }
179
180
181
182
183 public static final class SubDependencyThatInherits extends SecondDependency {
184 }
185
186
187
188
189 public static final class SubDependencyThatOverrides extends SecondDependency {
190 @Override
191 public int getValue() {
192 return 1;
193 }
194 }
195
196
197
198
199
200
201
202 @Test
203 void invocationOnBaseTypeWithReplayOnSubtypeThatOverridesTheInvokedMethod(@Mocked final SecondDependency mock2) {
204 new Expectations() {
205 {
206 mock1.getValue();
207 result = 15;
208 }
209 };
210
211
212 assertFalse(new TestedUnit().validate(mock1, new SubDependencyThatOverrides()));
213
214 new FullVerifications() {
215 {
216 mock2.getValue();
217 times = 0;
218 }
219 };
220 }
221
222
223
224
225
226
227
228 @Test
229 void invocationOnBaseTypeWithCapturingOfSubtypeThatInheritsTheInvokedMethod(
230 @Capturing final SecondDependency mock2) {
231 new Expectations() {
232 {
233 mock1.getValue();
234 result = 15;
235 mock2.getValue();
236 result = -50;
237 }
238 };
239
240 assertTrue(new TestedUnit().validate(mock1, new SubDependencyThatInherits()));
241 }
242
243
244
245
246
247
248
249 @Test
250 void invocationOnBaseTypeWithCapturingOfSubtypeThatOverridesTheInvokedMethod(
251 @Capturing final SecondDependency mock2) {
252 new Expectations() {
253 {
254 mock1.getValue();
255 result = 15;
256 mock2.getValue();
257 result = -50;
258 }
259 };
260
261 assertTrue(new TestedUnit().validate(mock1, new SubDependencyThatOverrides()));
262
263 new VerificationsInOrder() {
264 {
265 mock1.getValue();
266 mock2.getValue();
267 }
268 };
269 }
270
271
272
273
274
275
276
277
278
279
280
281
282 @Test
283 void invocationsOnCapturedImplementationsOfInterfaces(@Capturing final Callable<String> callable,
284 @Capturing final Observer observer) throws Exception {
285 new Expectations() {
286 {
287 observer.update(null, any);
288 times = 1;
289 }
290 };
291
292 TestedUnit.doSomethingWithInternallyCreatedImplementations();
293
294 new Verifications() {
295 {
296 callable.call();
297 }
298 };
299 }
300 }