1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.concurrentmockito;
7 
8 import org.junit.Test;
9 import org.mockitousage.IMethods;
10 import org.mockitoutil.TestBase;
11 
12 import static org.mockito.Mockito.mock;
13 import static org.mockito.Mockito.when;
14 
15 //this test always passes but please keep looking sys err
16 //this test should be run 10 times, manually
17 public class ThreadsShareGenerouslyStubbedMockTest extends TestBase {
18 
19     private IMethods mock;
20 
21     @Test
shouldAllowVerifyingInThreads()22     public void shouldAllowVerifyingInThreads() throws Exception {
23         for(int i = 0; i < 50; i++) {
24             performTest();
25         }
26     }
27 
performTest()28     private void performTest() throws InterruptedException {
29         mock = mock(IMethods.class);
30 
31         when(mock.simpleMethod("foo"))
32             .thenReturn("foo")
33             .thenReturn("bar")
34             .thenReturn("baz")
35             .thenReturn("foo")
36             .thenReturn("bar")
37             .thenReturn("baz");
38 
39         final Thread[] listeners = new Thread[100];
40         for (int i = 0; i < listeners.length; i++) {
41             listeners[i] = new Thread() {
42                 @Override
43                 public void run() {
44                     try {
45                         mock.simpleMethod("foo");
46                         mock.simpleMethod("foo");
47                         mock.simpleMethod("foo");
48                         mock.simpleMethod("foo");
49                         mock.simpleMethod("foo");
50                         mock.simpleMethod("foo");
51                     } catch (Exception e) {
52                         throw new RuntimeException(e);
53                     }
54                 }
55             };
56             listeners[i].start();
57         }
58         for (Thread listener : listeners) {
59             listener.join();
60         }
61     }
62 }
63