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.mockito.Mock; 10 import org.mockitousage.IMethods; 11 import org.mockitoutil.TestBase; 12 13 import static org.mockito.Mockito.atLeastOnce; 14 import static org.mockito.Mockito.verify; 15 16 //this test exposes the problem most of the time 17 public class ThreadVerifiesContinuoslyInteractingMockTest extends TestBase { 18 19 @Mock private IMethods mock; 20 21 @Test shouldAllowVerifyingInThreads()22 public void shouldAllowVerifyingInThreads() throws Exception { 23 for(int i = 0; i < 100; i++) { 24 performTest(); 25 } 26 } 27 performTest()28 private void performTest() throws InterruptedException { 29 mock.simpleMethod(); 30 final Thread[] listeners = new Thread[2]; 31 for (int i = 0; i < listeners.length; i++) { 32 final int x = i; 33 listeners[i] = new Thread() { 34 @Override 35 public void run() { 36 try { 37 Thread.sleep(x * 10); 38 } catch (InterruptedException e) { 39 throw new RuntimeException(e); 40 } 41 mock.simpleMethod(); 42 } 43 }; 44 listeners[i].start(); 45 } 46 47 verify(mock, atLeastOnce()).simpleMethod(); 48 49 for (Thread listener : listeners) { 50 listener.join(); 51 } 52 } 53 } 54