• 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 
6 package org.mockitousage.stubbing;
7 
8 import org.junit.Test;
9 import org.mockito.Mock;
10 import org.mockito.exceptions.base.MockitoException;
11 import org.mockitousage.IMethods;
12 import org.mockitoutil.TestBase;
13 
14 import static org.junit.Assert.*;
15 import static org.mockito.Mockito.*;
16 
17 public class StubbingConsecutiveAnswersTest extends TestBase {
18 
19     @Mock
20     private IMethods mock;
21 
22     @Test
should_return_consecutive_values()23     public void should_return_consecutive_values() throws Exception {
24         when(mock.simpleMethod())
25                 .thenReturn("one")
26                 .thenReturn("two")
27                 .thenReturn("three");
28 
29         assertEquals("one", mock.simpleMethod());
30         assertEquals("two", mock.simpleMethod());
31         assertEquals("three", mock.simpleMethod());
32         assertEquals("three", mock.simpleMethod());
33         assertEquals("three", mock.simpleMethod());
34     }
35 
36     @Test
should_return_consecutive_values_for_two_nulls()37     public void should_return_consecutive_values_for_two_nulls() throws Exception {
38         when(mock.simpleMethod()).thenReturn(null, (String[]) null);
39 
40         assertNull(mock.simpleMethod());
41         assertNull(mock.simpleMethod());
42     }
43 
44     @Test
should_return_consecutive_values_first_var_arg_null()45     public void should_return_consecutive_values_first_var_arg_null() throws Exception {
46         when(mock.simpleMethod()).thenReturn("one", (String) null);
47 
48         assertEquals("one", mock.simpleMethod());
49         assertNull(mock.simpleMethod());
50         assertNull(mock.simpleMethod());
51     }
52 
53     @Test
should_return_consecutive_values_var_arg_null()54     public void should_return_consecutive_values_var_arg_null() throws Exception {
55         when(mock.simpleMethod()).thenReturn("one", (String[]) null);
56 
57         assertEquals("one", mock.simpleMethod());
58         assertNull(mock.simpleMethod());
59         assertNull(mock.simpleMethod());
60     }
61 
62     @Test
should_return_consecutive_values_var_args_contain_null()63     public void should_return_consecutive_values_var_args_contain_null() throws Exception {
64         when(mock.simpleMethod()).thenReturn("one", "two", null);
65 
66         assertEquals("one", mock.simpleMethod());
67         assertEquals("two", mock.simpleMethod());
68         assertNull(mock.simpleMethod());
69         assertNull(mock.simpleMethod());
70     }
71 
72     @Test
should_return_consecutive_values_set_by_shorten_then_return_method()73     public void should_return_consecutive_values_set_by_shorten_then_return_method() throws Exception {
74         when(mock.simpleMethod()).thenReturn("one", "two", "three");
75 
76         assertEquals("one", mock.simpleMethod());
77         assertEquals("two", mock.simpleMethod());
78         assertEquals("three", mock.simpleMethod());
79         assertEquals("three", mock.simpleMethod());
80         assertEquals("three", mock.simpleMethod());
81     }
82 
83     @Test
should_return_consecutive_value_and_throw_exceptions_set_by_shorten_return_methods()84     public void should_return_consecutive_value_and_throw_exceptions_set_by_shorten_return_methods() {
85         when(mock.simpleMethod()).thenReturn("zero")
86                                  .thenReturn("one", "two")
87                                  .thenThrow(new NullPointerException(), new RuntimeException())
88                                  .thenReturn("three")
89                                  .thenThrow(new IllegalArgumentException());
90 
91         assertEquals("zero", mock.simpleMethod());
92         assertEquals("one", mock.simpleMethod());
93         assertEquals("two", mock.simpleMethod());
94         try {
95             mock.simpleMethod();
96             fail();
97         } catch (NullPointerException expected) { }
98         try {
99             mock.simpleMethod();
100             fail();
101         } catch (RuntimeException expected) { }
102         assertEquals("three", mock.simpleMethod());
103         try {
104             mock.simpleMethod();
105             fail();
106         } catch (IllegalArgumentException expected) { }
107     }
108 
109     @Test
should_throw_consecutively()110     public void should_throw_consecutively() throws Exception {
111         when(mock.simpleMethod()).thenThrow(new RuntimeException())
112                                  .thenThrow(new IllegalArgumentException())
113                                  .thenThrow(new NullPointerException());
114 
115         try {
116             mock.simpleMethod();
117             fail();
118         } catch (RuntimeException expected) { }
119 
120         try {
121             mock.simpleMethod();
122             fail();
123         } catch (IllegalArgumentException expected) { }
124 
125         try {
126             mock.simpleMethod();
127             fail();
128         } catch (NullPointerException expected) { }
129 
130         try {
131             mock.simpleMethod();
132             fail();
133         } catch (NullPointerException expected) { }
134     }
135 
136     @Test
should_throw_consecutively_set_by_shorten_then_throw_method()137     public void should_throw_consecutively_set_by_shorten_then_throw_method() throws Exception {
138         when(mock.simpleMethod()).thenThrow(new RuntimeException(),
139                                             new IllegalArgumentException(),
140                                             new NullPointerException());
141 
142         try {
143             mock.simpleMethod();
144             fail();
145         } catch (RuntimeException expected) { }
146 
147         try {
148             mock.simpleMethod();
149             fail();
150         } catch (IllegalArgumentException expected) { }
151 
152         try {
153             mock.simpleMethod();
154             fail();
155         } catch (NullPointerException expected) { }
156 
157         try {
158             mock.simpleMethod();
159             fail();
160         } catch (NullPointerException expected) { }
161     }
162 
163     @Test
should_throw_classes()164     public void should_throw_classes() throws Exception {
165         // Unavoidable JDK7+ 'unchecked generic array creation' warning
166         when(mock.simpleMethod()).thenThrow(IllegalArgumentException.class);
167 
168         try {
169             mock.simpleMethod();
170             fail();
171         } catch (IllegalArgumentException expected) { }
172 
173         try {
174             mock.simpleMethod();
175             fail();
176         } catch (IllegalArgumentException expected) { }
177     }
178 
179     @Test
180     @SuppressWarnings("unchecked")
should_throw_consecutively_classes_set_by_shorten_then_throw_method()181     public void should_throw_consecutively_classes_set_by_shorten_then_throw_method() throws Exception {
182         // Unavoidable JDK7+ 'unchecked generic array creation' warning
183         when(mock.simpleMethod()).thenThrow(RuntimeException.class,
184                                             IllegalArgumentException.class,
185                                             NullPointerException.class);
186 
187         try {
188             mock.simpleMethod();
189             fail();
190         } catch (RuntimeException expected) { }
191 
192         try {
193             mock.simpleMethod();
194             fail();
195         } catch (IllegalArgumentException expected) { }
196 
197         try {
198             mock.simpleMethod();
199             fail();
200         } catch (NullPointerException expected) { }
201 
202         try {
203             mock.simpleMethod();
204             fail();
205         } catch (NullPointerException expected) { }
206     }
207 
208     @Test
should_mix_consecutive_returns_with_exceptions()209     public void should_mix_consecutive_returns_with_exceptions() throws Exception {
210         when(mock.simpleMethod())
211                 .thenThrow(new IllegalArgumentException())
212                 .thenReturn("one")
213                 .thenThrow(new NullPointerException())
214                 .thenReturn(null);
215 
216         try {
217             mock.simpleMethod();
218             fail();
219         } catch (IllegalArgumentException expected) { }
220 
221         assertEquals("one", mock.simpleMethod());
222 
223         try {
224             mock.simpleMethod();
225             fail();
226         } catch (NullPointerException expected) { }
227 
228         assertEquals(null, mock.simpleMethod());
229         assertEquals(null, mock.simpleMethod());
230     }
231 
232     @Test(expected = MockitoException.class)
should_validate_consecutive_exception()233     public void should_validate_consecutive_exception() throws Exception {
234         when(mock.simpleMethod())
235                 .thenReturn("one")
236                 .thenThrow(new Exception());
237     }
238 
239     @Test
should_stub_void_method_and_continue_throwing()240     public void should_stub_void_method_and_continue_throwing() throws Exception {
241         doThrow(new IllegalArgumentException())
242         .doNothing()
243         .doThrow(new NullPointerException())
244         .when(mock).voidMethod();
245 
246         try {
247             mock.voidMethod();
248             fail();
249         } catch (IllegalArgumentException expected) { }
250 
251         mock.voidMethod();
252 
253         try {
254             mock.voidMethod();
255             fail();
256         } catch (NullPointerException expected) { }
257 
258         try {
259             mock.voidMethod();
260             fail();
261         } catch (NullPointerException expected) { }
262     }
263 
264     @Test
should_stub_void_method()265     public void should_stub_void_method() throws Exception {
266         doNothing()
267         .doThrow(new NullPointerException())
268         .doNothing()
269         .when(mock)
270         .voidMethod();
271 
272         mock.voidMethod();
273 
274         try {
275             mock.voidMethod();
276             fail();
277         } catch (NullPointerException expected) { }
278 
279         mock.voidMethod();
280         mock.voidMethod();
281     }
282 
283     @Test(expected = MockitoException.class)
should_validate_consecutive_exception_for_void_method()284     public void should_validate_consecutive_exception_for_void_method() throws Exception {
285         doNothing()
286         .doThrow(new Exception())
287         .when(mock)
288         .voidMethod();
289     }
290 }
291