• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.mockitousage.stubbing;
2 
3 import org.junit.After;
4 import org.junit.Test;
5 import org.mockito.Mock;
6 import org.mockito.Mockito;
7 import org.mockito.MockitoSession;
8 import org.mockito.exceptions.misusing.PotentialStubbingProblem;
9 import org.mockito.exceptions.misusing.UnnecessaryStubbingException;
10 import org.mockito.exceptions.verification.NoInteractionsWanted;
11 import org.mockito.quality.Strictness;
12 import org.mockitousage.IMethods;
13 
14 import static org.mockito.BDDMockito.given;
15 import static org.mockito.Mockito.verify;
16 import static org.mockito.Mockito.verifyNoMoreInteractions;
17 import static org.mockitoutil.ThrowableAssert.assertThat;
18 
19 public class StrictStubbingTest {
20 
21     @Mock IMethods mock;
22 
23     MockitoSession mockito = Mockito.mockitoSession().initMocks(this).strictness(Strictness.STRICT_STUBS).startMocking();
24 
after()25     @After public void after() {
26         //Some tests already invoke below but that's ok
27         mockito.finishMocking();
28     }
29 
no_interactions()30     @Test public void no_interactions() throws Throwable {
31         //expect no exception
32         mockito.finishMocking();
33     }
34 
few_interactions()35     @Test public void few_interactions() throws Throwable {
36         mock.simpleMethod(100);
37         mock.otherMethod();
38     }
39 
few_verified_interactions()40     @Test public void few_verified_interactions() throws Throwable {
41         //when
42         mock.simpleMethod(100);
43         mock.otherMethod();
44 
45         //and
46         verify(mock).simpleMethod(100);
47         verify(mock).otherMethod();
48         verifyNoMoreInteractions(mock);
49     }
50 
stubbed_method_is_implicitly_verified()51     @Test public void stubbed_method_is_implicitly_verified() throws Throwable {
52         //when
53         given(mock.simpleMethod(100)).willReturn("100");
54         mock.simpleMethod(100);
55 
56         //no exceptions:
57         verifyNoMoreInteractions(mock);
58     }
59 
unused_stubbed_is_not_implicitly_verified()60     @Test public void unused_stubbed_is_not_implicitly_verified() throws Throwable {
61         //when
62         given(mock.simpleMethod(100)).willReturn("100");
63         mock.simpleMethod(100); // <- implicitly verified
64         mock.simpleMethod(200); // <- unverified
65 
66         //expect
67         assertThat(new Runnable() {
68             public void run() {
69                 verifyNoMoreInteractions(mock);
70             }
71         }).throwsException(NoInteractionsWanted.class);
72     }
73 
stubbing_argument_mismatch()74     @Test public void stubbing_argument_mismatch() throws Throwable {
75         //when
76         given(mock.simpleMethod(100)).willReturn("100");
77 
78         //stubbing argument mismatch is detected
79         assertThat(new Runnable() {
80             public void run() {
81                 mock.simpleMethod(200);
82             }
83         }).throwsException(PotentialStubbingProblem.class);
84     }
85 
unused_stubbing()86     @Test public void unused_stubbing() throws Throwable {
87         //when
88         given(mock.simpleMethod(100)).willReturn("100");
89 
90         //unused stubbing is reported
91         assertThat(new Runnable() {
92             public void run() {
93                 mockito.finishMocking();
94             }
95         }).throwsException(UnnecessaryStubbingException.class);
96     }
97 }
98