1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.concurrentmockito; 6 7 import static org.mockito.Mockito.atLeastOnce; 8 import static org.mockito.Mockito.verify; 9 10 import org.junit.Test; 11 import org.mockito.Mock; 12 import org.mockitousage.IMethods; 13 import org.mockitoutil.TestBase; 14 15 // this test exposes the problem most of the time 16 public class ThreadVerifiesContinuouslyInteractingMockTest extends TestBase { 17 18 @Mock private IMethods mock; 19 20 @Test shouldAllowVerifyingInThreads()21 public void shouldAllowVerifyingInThreads() throws Exception { 22 for (int i = 0; i < 100; i++) { 23 performTest(); 24 } 25 } 26 performTest()27 private void performTest() throws InterruptedException { 28 mock.simpleMethod(); 29 final Thread[] listeners = new Thread[2]; 30 for (int i = 0; i < listeners.length; i++) { 31 final int x = i; 32 listeners[i] = 33 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