1 package org.mockitousage.junitrunner; 2 3 import org.junit.Test; 4 import org.junit.runner.JUnitCore; 5 import org.junit.runner.Result; 6 import org.junit.runner.RunWith; 7 import org.mockito.Mock; 8 import org.mockito.exceptions.misusing.PotentialStubbingProblem; 9 import org.mockito.exceptions.misusing.UnnecessaryStubbingException; 10 import org.mockito.junit.MockitoJUnitRunner; 11 import org.mockitousage.IMethods; 12 import org.mockitoutil.JUnitResultAssert; 13 import org.mockitoutil.TestBase; 14 15 import static org.junit.Assert.assertEquals; 16 import static org.mockito.Mockito.when; 17 18 public class StrictStubsRunnerTest extends TestBase { 19 20 JUnitCore runner = new JUnitCore(); 21 detects_unnecessary_stubbings()22 @Test public void detects_unnecessary_stubbings() { 23 //when 24 Result result = runner.run(UnnecessaryStubbing.class); 25 //then 26 JUnitResultAssert.assertThat(result) 27 .fails(1, UnnecessaryStubbingException.class) 28 .succeeds(2); 29 } 30 fails_fast_on_argument_mismatch()31 @Test public void fails_fast_on_argument_mismatch() { 32 //when 33 Result result = runner.run(StubbingArgMismatch.class); 34 //then 35 JUnitResultAssert.assertThat(result) 36 .succeeds(2) 37 .fails(1, PotentialStubbingProblem.class); 38 } 39 40 @RunWith(MockitoJUnitRunner.StrictStubs.class) 41 public static class UnnecessaryStubbing { 42 @Mock IMethods mock; unused_stubbing_1()43 @Test public void unused_stubbing_1() { 44 when(mock.simpleMethod()).thenReturn(""); 45 } unused_stubbing_2()46 @Test public void unused_stubbing_2() { 47 when(mock.simpleMethod()).thenReturn(""); 48 } correct_stubbing()49 @Test public void correct_stubbing() { 50 when(mock.simpleMethod()).thenReturn(""); 51 mock.simpleMethod(); 52 } 53 } 54 55 @RunWith(MockitoJUnitRunner.StrictStubs.class) 56 public static class StubbingArgMismatch { 57 @Mock IMethods mock; passing1()58 @Test public void passing1() {} passing2()59 @Test public void passing2() { 60 when(mock.simpleMethod()).thenReturn(""); 61 mock.simpleMethod(); 62 } argument_mismatch()63 @Test public void argument_mismatch() { 64 when(mock.simpleMethod(10)).thenReturn(""); 65 mock.simpleMethod(20); 66 } 67 } 68 } 69