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 junit.framework.TestCase.*; 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_set_by_shorten_then_return_method()45 public void should_return_consecutive_values_set_by_shorten_then_return_method() throws Exception { 46 when(mock.simpleMethod()).thenReturn("one", "two", "three"); 47 48 assertEquals("one", mock.simpleMethod()); 49 assertEquals("two", mock.simpleMethod()); 50 assertEquals("three", mock.simpleMethod()); 51 assertEquals("three", mock.simpleMethod()); 52 assertEquals("three", mock.simpleMethod()); 53 } 54 55 @Test should_return_consecutive_value_and_throw_exceptions_set_by_shorten_return_methods()56 public void should_return_consecutive_value_and_throw_exceptions_set_by_shorten_return_methods() { 57 when(mock.simpleMethod()).thenReturn("zero") 58 .thenReturn("one", "two") 59 .thenThrow(new NullPointerException(), new RuntimeException()) 60 .thenReturn("three") 61 .thenThrow(new IllegalArgumentException()); 62 63 assertEquals("zero", mock.simpleMethod()); 64 assertEquals("one", mock.simpleMethod()); 65 assertEquals("two", mock.simpleMethod()); 66 try { 67 mock.simpleMethod(); 68 fail(); 69 } catch (NullPointerException expected) { } 70 try { 71 mock.simpleMethod(); 72 fail(); 73 } catch (RuntimeException expected) { } 74 assertEquals("three", mock.simpleMethod()); 75 try { 76 mock.simpleMethod(); 77 fail(); 78 } catch (IllegalArgumentException expected) { } 79 } 80 81 @Test should_throw_consecutively()82 public void should_throw_consecutively() throws Exception { 83 when(mock.simpleMethod()).thenThrow(new RuntimeException()) 84 .thenThrow(new IllegalArgumentException()) 85 .thenThrow(new NullPointerException()); 86 87 try { 88 mock.simpleMethod(); 89 fail(); 90 } catch (RuntimeException expected) { } 91 92 try { 93 mock.simpleMethod(); 94 fail(); 95 } catch (IllegalArgumentException expected) { } 96 97 try { 98 mock.simpleMethod(); 99 fail(); 100 } catch (NullPointerException expected) { } 101 102 try { 103 mock.simpleMethod(); 104 fail(); 105 } catch (NullPointerException expected) { } 106 } 107 108 @Test should_throw_consecutively_set_by_shorten_then_throw_method()109 public void should_throw_consecutively_set_by_shorten_then_throw_method() throws Exception { 110 when(mock.simpleMethod()).thenThrow(new RuntimeException(), 111 new IllegalArgumentException(), 112 new NullPointerException()); 113 114 try { 115 mock.simpleMethod(); 116 fail(); 117 } catch (RuntimeException expected) { } 118 119 try { 120 mock.simpleMethod(); 121 fail(); 122 } catch (IllegalArgumentException expected) { } 123 124 try { 125 mock.simpleMethod(); 126 fail(); 127 } catch (NullPointerException expected) { } 128 129 try { 130 mock.simpleMethod(); 131 fail(); 132 } catch (NullPointerException expected) { } 133 } 134 135 @Test should_throw_classes()136 public void should_throw_classes() throws Exception { 137 // Unavoidable JDK7+ 'unchecked generic array creation' warning 138 when(mock.simpleMethod()).thenThrow(IllegalArgumentException.class); 139 140 try { 141 mock.simpleMethod(); 142 fail(); 143 } catch (IllegalArgumentException expected) { } 144 145 try { 146 mock.simpleMethod(); 147 fail(); 148 } catch (IllegalArgumentException expected) { } 149 } 150 151 @Test 152 @SuppressWarnings("unchecked") should_throw_consecutively_classes_set_by_shorten_then_throw_method()153 public void should_throw_consecutively_classes_set_by_shorten_then_throw_method() throws Exception { 154 // Unavoidable JDK7+ 'unchecked generic array creation' warning 155 when(mock.simpleMethod()).thenThrow(RuntimeException.class, 156 IllegalArgumentException.class, 157 NullPointerException.class); 158 159 try { 160 mock.simpleMethod(); 161 fail(); 162 } catch (RuntimeException expected) { } 163 164 try { 165 mock.simpleMethod(); 166 fail(); 167 } catch (IllegalArgumentException expected) { } 168 169 try { 170 mock.simpleMethod(); 171 fail(); 172 } catch (NullPointerException expected) { } 173 174 try { 175 mock.simpleMethod(); 176 fail(); 177 } catch (NullPointerException expected) { } 178 } 179 180 @Test should_mix_consecutive_returns_with_excepions()181 public void should_mix_consecutive_returns_with_excepions() throws Exception { 182 when(mock.simpleMethod()) 183 .thenThrow(new IllegalArgumentException()) 184 .thenReturn("one") 185 .thenThrow(new NullPointerException()) 186 .thenReturn(null); 187 188 try { 189 mock.simpleMethod(); 190 fail(); 191 } catch (IllegalArgumentException expected) { } 192 193 assertEquals("one", mock.simpleMethod()); 194 195 try { 196 mock.simpleMethod(); 197 fail(); 198 } catch (NullPointerException expected) { } 199 200 assertEquals(null, mock.simpleMethod()); 201 assertEquals(null, mock.simpleMethod()); 202 } 203 204 @Test(expected = MockitoException.class) should_validate_consecutive_exception()205 public void should_validate_consecutive_exception() throws Exception { 206 when(mock.simpleMethod()) 207 .thenReturn("one") 208 .thenThrow(new Exception()); 209 } 210 211 @Test should_stub_void_method_and_continue_throwing()212 public void should_stub_void_method_and_continue_throwing() throws Exception { 213 doThrow(new IllegalArgumentException()) 214 .doNothing() 215 .doThrow(new NullPointerException()) 216 .when(mock).voidMethod(); 217 218 try { 219 mock.voidMethod(); 220 fail(); 221 } catch (IllegalArgumentException expected) { } 222 223 mock.voidMethod(); 224 225 try { 226 mock.voidMethod(); 227 fail(); 228 } catch (NullPointerException expected) { } 229 230 try { 231 mock.voidMethod(); 232 fail(); 233 } catch (NullPointerException expected) { } 234 } 235 236 @Test should_stub_void_method()237 public void should_stub_void_method() throws Exception { 238 doNothing() 239 .doThrow(new NullPointerException()) 240 .doNothing() 241 .when(mock) 242 .voidMethod(); 243 244 mock.voidMethod(); 245 246 try { 247 mock.voidMethod(); 248 fail(); 249 } catch (NullPointerException expected) { } 250 251 mock.voidMethod(); 252 mock.voidMethod(); 253 } 254 255 @Test(expected = MockitoException.class) should_validate_consecutive_exception_for_void_method()256 public void should_validate_consecutive_exception_for_void_method() throws Exception { 257 doNothing() 258 .doThrow(new Exception()) 259 .when(mock) 260 .voidMethod(); 261 } 262 } 263