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