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