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