View Javadoc
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   * The Class MultiThreadedExpectationsTest.
17   */
18  @TestMethodOrder(MethodName.class)
19  final class MultiThreadedExpectationsTest {
20  
21      /**
22       * The Class Collaborator.
23       */
24      static class Collaborator {
25  
26          /**
27           * Do something.
28           *
29           * @return the int
30           */
31          int doSomething() {
32              return -1;
33          }
34  
35          /**
36           * Do something else.
37           */
38          void doSomethingElse() {
39          }
40      }
41  
42      /** The mock. */
43      @Mocked
44      Collaborator mock;
45  
46      /**
47       * Use mocked collaborator from worker thread.
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       * Use mocked object from worker thread while verifying expectation.
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       * Use mocked object from worker thread while recording and verifying expectation.
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      * Replay recorded expectation from another thread.
107      *
108      * @throws Exception
109      *             the exception
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      * The Class Dependency.
131      */
132     static class Dependency {
133 
134         /**
135          * Do something.
136          */
137         void doSomething() {
138         }
139 
140         /**
141          * Do something else.
142          */
143         static void doSomethingElse() {
144         }
145     }
146 
147     /**
148      * Verify invocations replayed in another thread whose class is no longer mocked.
149      *
150      * @param dep
151      *            the dep
152      * @param g2D
153      *            the g 2 D
154      * @param runnable
155      *            the runnable
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      * The Interface APublicInterface.
173      */
174     public interface APublicInterface {
175         /**
176          * Do something.
177          *
178          * @return true, if successful
179          */
180         boolean doSomething();
181     }
182 
183     /**
184      * Invoke method on mocked public interface from EDT.
185      *
186      * @param mock2
187      *            the mock 2
188      *
189      * @throws Exception
190      *             the exception
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      * The Class AnAbstractClass.
208      */
209     public abstract static class AnAbstractClass {
210         /**
211          * Do something.
212          *
213          * @return true, if successful
214          */
215         public abstract boolean doSomething();
216     }
217 
218     /**
219      * Invoke method on mocked abstract class from EDT.
220      *
221      * @param mock2
222      *            the mock 2
223      *
224      * @throws Exception
225      *             the exception
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      * Invoke method on mocked generic interface from EDT.
243      *
244      * @param mock2
245      *            the mock 2
246      *
247      * @throws Exception
248      *             the exception
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 }