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.assertNotSame;
10 import static org.junit.jupiter.api.Assertions.assertSame;
11 import static org.junit.jupiter.api.Assertions.fail;
12
13 import mockit.integration.junit5.ExpectedException;
14 import mockit.integration.junit5.JMockitExtension;
15 import mockit.internal.expectations.invocation.MissingInvocation;
16
17 import org.junit.jupiter.api.Assertions;
18 import org.junit.jupiter.api.Test;
19 import org.junit.jupiter.api.extension.ExtendWith;
20
21
22
23
24 @ExtendWith(JMockitExtension.class)
25 class MisusedMockingAPITest {
26
27
28
29
30 static class Blah {
31
32
33 @SuppressWarnings("RedundantStringConstructorCall")
34 final String name = "Blah";
35
36
37
38
39
40
41 int value() {
42 return -1;
43 }
44
45
46
47
48
49
50
51 void setValue(@SuppressWarnings("unused") int value) {
52 }
53
54
55
56
57
58
59
60
61
62 String doSomething(@SuppressWarnings("unused") boolean b) {
63 return "";
64 }
65
66
67
68
69
70
71 Runnable getSomethingElse() {
72 return null;
73 }
74 }
75
76
77 @Mocked
78 Blah mock;
79
80
81
82
83
84
85
86 @Test
87 void recordExpectationAfterInvokingSameMethodInReplayPhase() {
88 Assertions.assertEquals(0, mock.value());
89
90 new Expectations() {
91 {
92 mock.value();
93 result = 1;
94 }
95 };
96
97 Assertions.assertEquals(1, mock.value());
98 }
99
100
101
102
103
104
105
106 @Test
107 void recordDuplicateInvocationWithNoArguments() {
108 new Expectations() {
109 {
110 mock.value();
111 result = 1;
112 mock.value();
113 result = 2;
114 }
115 };
116
117 Assertions.assertEquals(2, mock.value());
118 Assertions.assertEquals(2, mock.value());
119 }
120
121
122
123
124 @Test
125 void recordDuplicateInvocationWithArgumentMatcher() {
126 new Expectations() {
127 {
128 mock.setValue(anyInt);
129 result = new UnknownError();
130 mock.setValue(anyInt);
131 }
132 };
133
134 mock.setValue(3);
135 }
136
137
138
139
140 @Test
141 void recordDuplicateInvocationInSeparateExpectationBlocks() {
142 new Expectations() {
143 {
144 mock.value();
145 result = 1;
146 }
147 };
148 new Expectations() {
149 {
150 mock.value();
151 result = 2;
152 }
153 };
154
155 Assertions.assertEquals(2, mock.value());
156 }
157
158
159
160
161 @Test
162 @ExpectedException(MissingInvocation.class)
163 void recordMockInstanceForConstructorExpectation() {
164 Throwable exception = Assertions.assertThrows(IllegalArgumentException.class, () -> {
165 new Expectations() {
166 {
167 new Blah();
168 result = mock;
169 }
170 };
171
172 new Blah();
173 });
174
175 Assertions.assertTrue(exception.getMessage().contains("Invalid assignment"));
176 Assertions.assertTrue(exception.getMessage().contains("result"));
177 Assertions.assertTrue(exception.getMessage().contains("constructor"));
178 }
179
180
181
182
183
184
185
186
187
188
189 @Test
190 void recordUnorderedInstantiationOfClassMockedTwice(@Mocked final Blah mock2) {
191 new Expectations() {
192 {
193 new Blah();
194 times = 1;
195 mock.value();
196 result = 123;
197 mock2.value();
198 result = 45;
199 }
200 };
201
202 Assertions.assertEquals(45, mock2.value());
203 Assertions.assertEquals(123, mock.value());
204 new Blah();
205 }
206
207
208
209
210
211
212
213 @Test
214 void verifyOrderedInstantiationOfClassMockedTwice(@Mocked final Blah mock2) {
215 new Blah();
216 mock2.doSomething(true);
217
218 new VerificationsInOrder() {
219 {
220 new Blah();
221 mock2.doSomething(anyBoolean);
222 }
223 };
224 }
225
226
227
228
229
230
231
232 @Test
233 void verifyUnorderedInstantiationOfClassMockedTwice(@Mocked final Blah mock2) {
234 mock.doSomething(false);
235 mock2.doSomething(true);
236 new Blah();
237
238 new Verifications() {
239 {
240 mock2.doSomething(true);
241 new Blah();
242 mock.doSomething(false);
243 }
244 };
245 }
246
247
248
249
250
251
252
253
254
255
256
257
258 @Test
259 void ambiguousCascadingWhenMultipleValidCandidatesAreAvailable(@Injectable Runnable r1, @Injectable Runnable r2) {
260 Runnable cascaded = mock.getSomethingElse();
261
262 Assertions.assertSame(r2, cascaded);
263 }
264
265
266
267
268
269
270
271 static class TestedClass {
272
273 Runnable action;
274 }
275
276
277 @Injectable
278 static Runnable mockAction = new Runnable() {
279 @Override
280 public void run() {
281 }
282 };
283
284
285 @Tested
286 TestedClass tested;
287
288
289
290
291 @Test
292 void checkStaticInjectableIsNotUsed() {
293 Assertions.assertNotSame(mockAction, tested.action);
294 }
295
296
297
298
299
300
301
302 static final class CustomException extends Exception {
303
304 private static final long serialVersionUID = 1L;
305 }
306
307
308
309
310
311
312
313 @Test
314 @ExpectedException(IllegalArgumentException.class)
315 void attemptingToMockAllInstancesOfExceptionSubclass(@Mocked CustomException anyCustomException) {
316 fail("Shouldn't get here");
317 }
318 }