• 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.Before;
9 import org.junit.Test;
10 import org.mockito.exceptions.base.MockitoException;
11 import org.mockito.exceptions.verification.NoInteractionsWanted;
12 import org.mockito.exceptions.verification.WantedButNotInvoked;
13 import org.mockitoutil.TestBase;
14 
15 import java.io.IOException;
16 import java.io.Reader;
17 import java.util.HashMap;
18 import java.util.LinkedList;
19 import java.util.Map;
20 
21 import static junit.framework.TestCase.*;
22 import static org.mockito.Mockito.*;
23 
24 @SuppressWarnings({"serial", "unchecked", "all", "deprecation"})
25 public class StubbingWithThrowablesTest extends TestBase {
26 
27     private LinkedList mock;
28 
29     private Map mockTwo;
30 
31     @Before
setup()32     public void setup() {
33         mock = mock(LinkedList.class);
34         mockTwo = mock(HashMap.class);
35     }
36 
37     @Test
shouldStubWithThrowable()38     public void shouldStubWithThrowable() throws Exception {
39         IllegalArgumentException expected = new IllegalArgumentException("thrown by mock");
40         when(mock.add("throw")).thenThrow(expected);
41 
42         try {
43             mock.add("throw");
44             fail();
45         } catch (IllegalArgumentException e) {
46             assertEquals(expected, e);
47         }
48     }
49 
50     @Test
shouldSetThrowableToVoidMethod()51     public void shouldSetThrowableToVoidMethod() throws Exception {
52         IllegalArgumentException expected = new IllegalArgumentException("thrown by mock");
53 
54         doThrow(expected).when(mock).clear();
55         try {
56             mock.clear();
57             fail();
58         } catch (Exception e) {
59             assertEquals(expected, e);
60         }
61     }
62 
63     @Test
shouldLastStubbingVoidBeImportant()64     public void shouldLastStubbingVoidBeImportant() throws Exception {
65         doThrow(new ExceptionOne()).when(mock).clear();
66         doThrow(new ExceptionTwo()).when(mock).clear();
67 
68         try {
69             mock.clear();
70             fail();
71         } catch (ExceptionTwo e) {
72         }
73     }
74 
75     @Test
shouldFailStubbingThrowableOnTheSameInvocationDueToAcceptableLimitation()76     public void shouldFailStubbingThrowableOnTheSameInvocationDueToAcceptableLimitation() throws Exception {
77         when(mock.get(1)).thenThrow(new ExceptionOne());
78 
79         try {
80             when(mock.get(1)).thenThrow(new ExceptionTwo());
81             fail();
82         } catch (ExceptionOne e) {
83         }
84     }
85 
86     @Test
shouldAllowSettingCheckedException()87     public void shouldAllowSettingCheckedException() throws Exception {
88         Reader reader = mock(Reader.class);
89         IOException ioException = new IOException();
90 
91         when(reader.read()).thenThrow(ioException);
92 
93         try {
94             reader.read();
95             fail();
96         } catch (Exception e) {
97             assertEquals(ioException, e);
98         }
99     }
100 
101     @Test
shouldAllowSettingError()102     public void shouldAllowSettingError() throws Exception {
103         Error error = new Error();
104 
105         when(mock.add("quake")).thenThrow(error);
106 
107         try {
108             mock.add("quake");
109             fail();
110         } catch (Error e) {
111             assertEquals(error, e);
112         }
113     }
114 
115     @Test(expected = MockitoException.class)
shouldNotAllowNullExceptionType()116     public void shouldNotAllowNullExceptionType() {
117         when(mock.add(null)).thenThrow((Exception) null);
118     }
119 
120 
121     @Test(expected = NaughtyException.class)
shouldInstantiateExceptionClassOnInteraction()122     public void shouldInstantiateExceptionClassOnInteraction() {
123         when(mock.add(null)).thenThrow(NaughtyException.class);
124 
125         mock.add(null);
126     }
127 
128     @Test(expected = NaughtyException.class)
shouldInstantiateExceptionClassWithOngoingStubbingOnInteraction()129     public void shouldInstantiateExceptionClassWithOngoingStubbingOnInteraction() {
130         doThrow(NaughtyException.class).when(mock).add(null);
131 
132         mock.add(null);
133     }
134 
135     @Test(expected = MockitoException.class)
shouldNotAllowSettingInvalidCheckedException()136     public void shouldNotAllowSettingInvalidCheckedException() throws Exception {
137         when(mock.add("monkey island")).thenThrow(new Exception());
138     }
139 
140     @Test(expected = MockitoException.class)
shouldNotAllowSettingNullThrowable()141     public void shouldNotAllowSettingNullThrowable() throws Exception {
142         when(mock.add("monkey island")).thenThrow((Throwable) null);
143     }
144 
145     @Test(expected = MockitoException.class)
shouldNotAllowSettingNullThrowableArray()146     public void shouldNotAllowSettingNullThrowableArray() throws Exception {
147         when(mock.add("monkey island")).thenThrow((Throwable[]) null);
148     }
149 
150     @Test
shouldMixThrowablesAndReturnsOnDifferentMocks()151     public void shouldMixThrowablesAndReturnsOnDifferentMocks() throws Exception {
152         when(mock.add("ExceptionOne")).thenThrow(new ExceptionOne());
153         when(mock.getLast()).thenReturn("last");
154         doThrow(new ExceptionTwo()).when(mock).clear();
155 
156         doThrow(new ExceptionThree()).when(mockTwo).clear();
157         when(mockTwo.containsValue("ExceptionFour")).thenThrow(new ExceptionFour());
158         when(mockTwo.get("Are you there?")).thenReturn("Yes!");
159 
160         assertNull(mockTwo.get("foo"));
161         assertTrue(mockTwo.keySet().isEmpty());
162         assertEquals("Yes!", mockTwo.get("Are you there?"));
163         try {
164             mockTwo.clear();
165             fail();
166         } catch (ExceptionThree e) {
167         }
168         try {
169             mockTwo.containsValue("ExceptionFour");
170             fail();
171         } catch (ExceptionFour e) {
172         }
173 
174         assertNull(mock.getFirst());
175         assertEquals("last", mock.getLast());
176         try {
177             mock.add("ExceptionOne");
178             fail();
179         } catch (ExceptionOne e) {
180         }
181         try {
182             mock.clear();
183             fail();
184         } catch (ExceptionTwo e) {
185         }
186     }
187 
188     @Test
shouldStubbingWithThrowableBeVerifiable()189     public void shouldStubbingWithThrowableBeVerifiable() {
190         when(mock.size()).thenThrow(new RuntimeException());
191         doThrow(new RuntimeException()).when(mock).clone();
192 
193         try {
194             mock.size();
195             fail();
196         } catch (RuntimeException e) {
197         }
198 
199         try {
200             mock.clone();
201             fail();
202         } catch (RuntimeException e) {
203         }
204 
205         verify(mock).size();
206         verify(mock).clone();
207         verifyNoMoreInteractions(mock);
208     }
209 
210     @Test
shouldStubbingWithThrowableFailVerification()211     public void shouldStubbingWithThrowableFailVerification() {
212         when(mock.size()).thenThrow(new RuntimeException());
213         doThrow(new RuntimeException()).when(mock).clone();
214 
215         verifyZeroInteractions(mock);
216 
217         mock.add("test");
218 
219         try {
220             verify(mock).size();
221             fail();
222         } catch (WantedButNotInvoked e) {
223         }
224 
225         try {
226             verify(mock).clone();
227             fail();
228         } catch (WantedButNotInvoked e) {
229         }
230 
231         try {
232             verifyNoMoreInteractions(mock);
233             fail();
234         } catch (NoInteractionsWanted e) {
235         }
236     }
237 
238     private class ExceptionOne extends RuntimeException {
239 
240     }
241 
242     private class ExceptionTwo extends RuntimeException {
243 
244     }
245 
246     private class ExceptionThree extends RuntimeException {
247 
248     }
249 
250     private class ExceptionFour extends RuntimeException {
251 
252     }
253 
254     public class NaughtyException extends RuntimeException {
255 
NaughtyException()256         public NaughtyException() {
257             throw new RuntimeException("boo!");
258         }
259     }
260 
261     @Test(expected = NaughtyException.class)
shouldShowDecentMessageWhenExcepionIsNaughty()262     public void shouldShowDecentMessageWhenExcepionIsNaughty() throws Exception {
263         when(mock.add("")).thenThrow(NaughtyException.class);
264         mock.add("");
265     }
266 }
267