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