1 package mockit;
2
3 import static org.junit.jupiter.api.Assertions.assertEquals;
4 import static org.junit.jupiter.api.Assertions.assertTrue;
5
6 import java.awt.Graphics2D;
7 import java.util.concurrent.Callable;
8
9 import javax.swing.SwingUtilities;
10
11 import org.junit.jupiter.api.MethodOrderer.MethodName;
12 import org.junit.jupiter.api.Test;
13 import org.junit.jupiter.api.TestMethodOrder;
14
15
16
17
18 @TestMethodOrder(MethodName.class)
19 final class MultiThreadedExpectationsTest {
20
21
22
23
24 static class Collaborator {
25
26
27
28
29
30
31 int doSomething() {
32 return -1;
33 }
34
35
36
37
38 void doSomethingElse() {
39 }
40 }
41
42
43 @Mocked
44 Collaborator mock;
45
46
47
48
49 void useMockedCollaboratorFromWorkerThread() {
50 Thread worker = new Thread() {
51 @Override
52 public void run() {
53 mock.doSomethingElse();
54 }
55 };
56 worker.start();
57 try {
58 worker.join();
59 } catch (InterruptedException ignore) {
60 }
61 }
62
63
64
65
66 @Test
67 void useMockedObjectFromWorkerThreadWhileVerifyingExpectation() {
68 mock.doSomething();
69 mock.doSomething();
70
71 new Verifications() {
72 {
73 mock.doSomething();
74 useMockedCollaboratorFromWorkerThread();
75 times = 2;
76 }
77 };
78 }
79
80
81
82
83 @Test
84 void useMockedObjectFromWorkerThreadWhileRecordingAndVerifyingExpectation() {
85 new Expectations() {
86 {
87 mock.doSomething();
88 useMockedCollaboratorFromWorkerThread();
89 result = 123;
90 }
91 };
92
93 assertEquals(123, mock.doSomething());
94 mock.doSomethingElse();
95
96 new VerificationsInOrder() {
97 {
98 useMockedCollaboratorFromWorkerThread();
99 mock.doSomething();
100 mock.doSomethingElse();
101 }
102 };
103 }
104
105
106
107
108
109
110
111 @Test
112 void replayRecordedExpectationFromAnotherThread() throws Exception {
113 new Expectations() {
114 {
115 mock.doSomething();
116 }
117 };
118
119 Thread task = new Thread() {
120 @Override
121 public void run() {
122 mock.doSomething();
123 }
124 };
125 task.start();
126 task.join();
127 }
128
129
130
131
132 static class Dependency {
133
134
135
136
137 void doSomething() {
138 }
139
140
141
142
143 static void doSomethingElse() {
144 }
145 }
146
147
148
149
150
151
152
153
154
155
156
157 @Test
158 void verifyInvocationsReplayedInAnotherThreadWhoseClassIsNoLongerMocked(@Mocked final Dependency dep,
159 @Mocked final Graphics2D g2D, @Mocked final Runnable runnable) {
160 new Thread() {
161 @Override
162 public void run() {
163 dep.doSomething();
164 g2D.dispose();
165 runnable.run();
166 Dependency.doSomethingElse();
167 }
168 }.start();
169 }
170
171
172
173
174 public interface APublicInterface {
175
176
177
178
179
180 boolean doSomething();
181 }
182
183
184
185
186
187
188
189
190
191
192 @Test
193 void invokeMethodOnMockedPublicInterfaceFromEDT(@Mocked final APublicInterface mock2) throws Exception {
194 new Expectations() {
195 {
196 mock2.doSomething();
197 result = true;
198 }
199 };
200
201 SwingUtilities.invokeAndWait(() -> assertTrue(mock2.doSomething()));
202
203 assertTrue(mock2.doSomething());
204 }
205
206
207
208
209 public abstract static class AnAbstractClass {
210
211
212
213
214
215 public abstract boolean doSomething();
216 }
217
218
219
220
221
222
223
224
225
226
227 @Test
228 void invokeMethodOnMockedAbstractClassFromEDT(@Mocked final AnAbstractClass mock2) throws Exception {
229 new Expectations() {
230 {
231 mock2.doSomething();
232 result = true;
233 }
234 };
235
236 SwingUtilities.invokeAndWait(() -> assertTrue(mock2.doSomething()));
237
238 assertTrue(mock2.doSomething());
239 }
240
241
242
243
244
245
246
247
248
249
250 @Test
251 void invokeMethodOnMockedGenericInterfaceFromEDT(@Mocked final Callable<Boolean> mock2) throws Exception {
252 new Expectations() {
253 {
254 mock2.call();
255 result = true;
256 }
257 };
258
259 SwingUtilities.invokeAndWait(() -> {
260 try {
261 assertTrue(mock2.call());
262 } catch (Exception e) {
263 throw new RuntimeException(e);
264 }
265 });
266
267 assertTrue(mock2.call());
268 }
269 }