1 package org.mockitousage.junitrule; 2 3 import org.junit.Rule; 4 import org.junit.Test; 5 import org.junit.runner.JUnitCore; 6 import org.junit.runner.Result; 7 import org.mockito.Mock; 8 import org.mockito.junit.MockitoJUnit; 9 import org.mockito.junit.MockitoRule; 10 import org.mockito.quality.Strictness; 11 import org.mockitousage.IMethods; 12 import org.mockitoutil.JUnitResultAssert; 13 14 import static org.mockito.Mockito.when; 15 16 public class MutableStrictJUnitRuleTest { 17 18 JUnitCore runner = new JUnitCore(); 19 20 @Test public void rule_can_be_changed_to_strict() throws Throwable { 21 //when 22 Result result = runner.run(LenientByDefault.class); 23 24 //then 25 JUnitResultAssert.assertThat(result) 26 .succeeds(1) 27 .fails(1, RuntimeException.class); 28 } 29 30 @Test public void rule_can_be_changed_to_lenient() throws Throwable { 31 //when 32 Result result = runner.run(StrictByDefault.class); 33 34 //then 35 JUnitResultAssert.assertThat(result) 36 .succeeds(1) 37 .fails(1, RuntimeException.class); 38 } 39 40 public static class LenientByDefault { 41 @Rule public MockitoRule mockito = MockitoJUnit.rule().strictness(Strictness.LENIENT); 42 @Mock IMethods mock; 43 44 @Test public void unused_stub() throws Throwable { 45 when(mock.simpleMethod()).thenReturn("1"); 46 } 47 48 @Test public void unused_stub_with_strictness() throws Throwable { 49 //making Mockito strict only for this test method 50 mockito.strictness(Strictness.STRICT_STUBS); 51 52 when(mock.simpleMethod()).thenReturn("1"); 53 } 54 } 55 56 public static class StrictByDefault { 57 @Rule public MockitoRule mockito = MockitoJUnit.rule().strictness(Strictness.STRICT_STUBS); 58 @Mock IMethods mock; 59 60 @Test public void unused_stub() throws Throwable { 61 when(mock.simpleMethod()).thenReturn("1"); 62 } 63 64 @Test public void unused_stub_with_lenient() throws Throwable { 65 //making Mockito lenient only for this test method 66 mockito.strictness(Strictness.LENIENT); 67 68 when(mock.simpleMethod()).thenReturn("1"); 69 } 70 } 71 } 72