1 /* 2 * Copyright (c) 2007 Mockito contributors 3 * This program is made available under the terms of the MIT License. 4 */ 5 package org.mockito.internal.stubbing; 6 7 import org.mockito.internal.stubbing.answers.CallsRealMethods; 8 import org.mockito.internal.stubbing.answers.Returns; 9 import org.mockito.internal.stubbing.answers.ThrowsException; 10 import org.mockito.internal.stubbing.answers.ThrowsExceptionClass; 11 import org.mockito.stubbing.OngoingStubbing; 12 13 public abstract class BaseStubbing<T> implements OngoingStubbing<T> { 14 15 public OngoingStubbing<T> thenReturn(T value) { 16 return thenAnswer(new Returns(value)); 17 } 18 19 @SuppressWarnings({"unchecked","vararg"}) 20 public OngoingStubbing<T> thenReturn(T value, T... values) { 21 OngoingStubbing<T> stubbing = thenReturn(value); 22 if (values == null) { 23 //TODO below does not seem right 24 return stubbing.thenReturn(null); 25 } 26 for (T v: values) { 27 stubbing = stubbing.thenReturn(v); 28 } 29 return stubbing; 30 } 31 32 private OngoingStubbing<T> thenThrow(Throwable throwable) { 33 return thenAnswer(new ThrowsException(throwable)); 34 } 35 36 public OngoingStubbing<T> thenThrow(Throwable... throwables) { 37 if (throwables == null) { 38 return thenThrow((Throwable) null); 39 } 40 OngoingStubbing<T> stubbing = null; 41 for (Throwable t: throwables) { 42 if (stubbing == null) { 43 stubbing = thenThrow(t); 44 } else { 45 stubbing = stubbing.thenThrow(t); 46 } 47 } 48 return stubbing; 49 } 50 51 public OngoingStubbing<T> thenThrow(Class<? extends Throwable> throwableType) { 52 return thenAnswer(new ThrowsExceptionClass(throwableType)); 53 } 54 55 @SuppressWarnings ({"unchecked", "varargs"}) 56 public OngoingStubbing<T> thenThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown) { 57 if (nextToBeThrown == null) { 58 thenThrow((Throwable) null); 59 } 60 OngoingStubbing<T> stubbing = thenThrow(toBeThrown); 61 for (Class<? extends Throwable> t: nextToBeThrown) { 62 stubbing = stubbing.thenThrow(t); 63 } 64 return stubbing; 65 } 66 67 public OngoingStubbing<T> thenCallRealMethod() { 68 return thenAnswer(new CallsRealMethods()); 69 } 70 } 71