1 package org.mockitousage.junitrule; 2 3 import org.junit.Rule; 4 import org.junit.Test; 5 import org.mockito.Mock; 6 import org.mockito.quality.Strictness; 7 import org.mockito.internal.junit.JUnitRule; 8 import org.mockito.internal.util.SimpleMockitoLogger; 9 import org.mockitousage.IMethods; 10 import org.mockitoutil.ConcurrentTesting; 11 import org.mockitoutil.SafeJUnitRule; 12 13 import static org.junit.Assert.assertEquals; 14 import static org.junit.Assert.assertTrue; 15 import static org.mockito.Mockito.when; 16 import static org.mockitoutil.TestBase.filterLineNo; 17 18 public class StubbingWarningsMultiThreadingTest { 19 20 private SimpleMockitoLogger logger = new SimpleMockitoLogger(); 21 @Rule public SafeJUnitRule rule = new SafeJUnitRule(new JUnitRule(logger, Strictness.WARN)); 22 @Mock IMethods mock; 23 using_stubbing_from_different_thread()24 @Test public void using_stubbing_from_different_thread() throws Throwable { 25 //expect no warnings 26 rule.expectSuccess(new Runnable() { 27 public void run() { 28 assertTrue(logger.getLoggedInfo().isEmpty()); 29 } 30 }); 31 32 //when stubbing is declared 33 when(mock.simpleMethod()).thenReturn("1"); 34 //and used from a different thread 35 ConcurrentTesting.inThread(new Runnable() { 36 public void run() { 37 mock.simpleMethod(); 38 } 39 }); 40 } 41 unused_stub_from_different_thread()42 @Test public void unused_stub_from_different_thread() throws Throwable { 43 //expect warnings 44 rule.expectSuccess(new Runnable() { 45 public void run() { 46 assertEquals( 47 "[MockitoHint] StubbingWarningsMultiThreadingTest.unused_stub_from_different_thread (see javadoc for MockitoHint):\n" + 48 "[MockitoHint] 1. Unused -> at org.mockitousage.junitrule.StubbingWarningsMultiThreadingTest.unused_stub_from_different_thread(StubbingWarningsMultiThreadingTest.java:0)\n", 49 filterLineNo(logger.getLoggedInfo())); 50 } 51 }); 52 53 //when stubbings are declared 54 when(mock.simpleMethod(1)).thenReturn("1"); 55 when(mock.simpleMethod(2)).thenReturn("2"); 56 57 //and one of the stubbings is used from a different thread 58 ConcurrentTesting.inThread(new Runnable() { 59 public void run() { 60 mock.simpleMethod(1); 61 } 62 }); 63 } 64 65 } 66