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.*; 13 14 public class ThreadsShareAMockTest extends TestBase { 15 16 private IMethods mock; 17 18 @Test 19 public void shouldAllowVerifyingInThreads() throws Exception { 20 for(int i = 0; i < 100; i++) { 21 performTest(); 22 } 23 } 24 25 private void performTest() throws InterruptedException { 26 mock = mock(IMethods.class); 27 final Thread[] listeners = new Thread[3]; 28 for (int i = 0; i < listeners.length; i++) { 29 listeners[i] = new Thread() { 30 @Override 31 public void run() { 32 mock.simpleMethod("foo"); 33 } 34 }; 35 listeners[i].start(); 36 } 37 for (Thread listener : listeners) { 38 listener.join(); 39 } 40 verify(mock, times(listeners.length)).simpleMethod("foo"); 41 } 42 } 43