• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockitousage.stubbing;
6 
7 import static org.assertj.core.api.Assertions.assertThat;
8 import static org.junit.Assert.assertEquals;
9 import static org.junit.Assert.fail;
10 import static org.mockito.Mockito.*;
11 
12 import org.junit.Before;
13 import org.junit.Test;
14 import org.mockito.exceptions.misusing.CannotVerifyStubOnlyMock;
15 import org.mockito.exceptions.misusing.MissingMethodInvocationException;
16 import org.mockito.exceptions.verification.NoInteractionsWanted;
17 import org.mockitousage.IMethods;
18 import org.mockitoutil.TestBase;
19 
20 public class BasicStubbingTest extends TestBase {
21 
22     private IMethods mock;
23 
24     @Before
setup()25     public void setup() {
26         mock = mock(IMethods.class);
27     }
28 
29     @Test
should_evaluate_latest_stubbing_first()30     public void should_evaluate_latest_stubbing_first() throws Exception {
31         when(mock.objectReturningMethod(isA(Integer.class))).thenReturn(100);
32         when(mock.objectReturningMethod(200)).thenReturn(200);
33 
34         assertEquals(200, mock.objectReturningMethod(200));
35         assertEquals(100, mock.objectReturningMethod(666));
36         assertEquals(
37                 "default behavior should return null", null, mock.objectReturningMethod("blah"));
38     }
39 
40     @Test
should_stubbing_be_treated_as_interaction()41     public void should_stubbing_be_treated_as_interaction() throws Exception {
42         when(mock.booleanReturningMethod()).thenReturn(true);
43 
44         mock.booleanReturningMethod();
45 
46         try {
47             verifyNoMoreInteractions(mock);
48             fail();
49         } catch (NoInteractionsWanted e) {
50         }
51     }
52 
53     @Test
should_allow_stubbing_to_string()54     public void should_allow_stubbing_to_string() throws Exception {
55         IMethods mockTwo = mock(IMethods.class);
56         when(mockTwo.toString()).thenReturn("test");
57 
58         assertThat(mock.toString()).contains("Mock for IMethods");
59         assertThat(mockTwo.toString()).isEqualTo("test");
60     }
61 
62     @Test
should_stubbing_not_be_treated_as_interaction()63     public void should_stubbing_not_be_treated_as_interaction() {
64         when(mock.simpleMethod("one")).thenThrow(new RuntimeException());
65         doThrow(new RuntimeException()).when(mock).simpleMethod("two");
66 
67         verifyNoInteractions(mock);
68     }
69 
70     @Test
should_stubbing_not_be_treated_as_interaction_verify_no_interactions()71     public void should_stubbing_not_be_treated_as_interaction_verify_no_interactions() {
72         when(mock.simpleMethod("one")).thenThrow(new RuntimeException());
73         doThrow(new RuntimeException()).when(mock).simpleMethod("two");
74 
75         verifyNoInteractions(mock);
76     }
77 
78     @Test
unfinished_stubbing_cleans_up_the_state()79     public void unfinished_stubbing_cleans_up_the_state() {
80         reset(mock);
81         try {
82             when("").thenReturn("");
83             fail();
84         } catch (MissingMethodInvocationException e) {
85         }
86 
87         // anything that can cause state validation
88         verifyNoInteractions(mock);
89     }
90 
91     @Test
unfinished_stubbing_cleans_up_the_state_verify_no_interactions()92     public void unfinished_stubbing_cleans_up_the_state_verify_no_interactions() {
93         reset(mock);
94         try {
95             when("").thenReturn("");
96             fail();
97         } catch (MissingMethodInvocationException e) {
98         }
99 
100         // anything that can cause state validation
101         verifyNoInteractions(mock);
102     }
103 
104     @Test
should_to_string_mock_name()105     public void should_to_string_mock_name() {
106         IMethods mock = mock(IMethods.class, "mockie");
107         IMethods mockTwo = mock(IMethods.class);
108 
109         assertThat(mockTwo.toString()).contains("Mock for IMethods");
110         assertEquals("mockie", "" + mock);
111     }
112 
113     class Foo {
toString()114         public final String toString() {
115             return "foo";
116         }
117     }
118 
119     @SuppressWarnings({"CheckReturnValue", "MockitoUsage"})
120     @Test
should_allow_mocking_when_to_string_is_final()121     public void should_allow_mocking_when_to_string_is_final() throws Exception {
122         mock(Foo.class);
123     }
124 
125     @Test
test_stub_only_not_verifiable()126     public void test_stub_only_not_verifiable() throws Exception {
127         IMethods localMock = mock(IMethods.class, withSettings().stubOnly());
128 
129         when(localMock.objectReturningMethod(isA(Integer.class))).thenReturn(100);
130         when(localMock.objectReturningMethod(200)).thenReturn(200);
131 
132         assertEquals(200, localMock.objectReturningMethod(200));
133         assertEquals(100, localMock.objectReturningMethod(666));
134         assertEquals(
135                 "default behavior should return null",
136                 null,
137                 localMock.objectReturningMethod("blah"));
138 
139         try {
140             verify(localMock, atLeastOnce()).objectReturningMethod(eq(200));
141             fail();
142         } catch (CannotVerifyStubOnlyMock e) {
143         }
144     }
145 
146     @SuppressWarnings("MockitoUsage")
147     @Test
test_stub_only_not_verifiable_fail_fast()148     public void test_stub_only_not_verifiable_fail_fast() {
149         IMethods localMock = mock(IMethods.class, withSettings().stubOnly());
150 
151         try {
152             verify(localMock); // throws exception before method invocation
153             fail();
154         } catch (CannotVerifyStubOnlyMock e) {
155             assertEquals(
156                     "\n"
157                             + "Argument \"iMethods\" passed to verify is a stubOnly() mock which cannot be verified.\n"
158                             + "If you intend to verify invocations on this mock, don't use stubOnly() in its MockSettings.",
159                     e.getMessage());
160         }
161     }
162 
163     @Test
test_stub_only_not_verifiable_verify_no_more_interactions()164     public void test_stub_only_not_verifiable_verify_no_more_interactions() {
165         IMethods localMock = mock(IMethods.class, withSettings().stubOnly());
166 
167         try {
168             verifyNoMoreInteractions(localMock);
169             fail();
170         } catch (CannotVerifyStubOnlyMock e) {
171         }
172     }
173 
174     @Test
test_stub_only_not_verifiable_in_order()175     public void test_stub_only_not_verifiable_in_order() {
176         IMethods localMock = mock(IMethods.class, withSettings().stubOnly());
177 
178         try {
179             inOrder(localMock);
180             fail();
181         } catch (CannotVerifyStubOnlyMock e) {
182         }
183     }
184 }
185