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 static org.mockito.internal.exceptions.Reporter.notAnException;
8 import static org.mockito.internal.progress.ThreadSafeMockingProgress.mockingProgress;
9 import static org.objenesis.ObjenesisHelper.newInstance;
10 
11 import org.mockito.internal.stubbing.answers.CallsRealMethods;
12 import org.mockito.internal.stubbing.answers.Returns;
13 import org.mockito.internal.stubbing.answers.ThrowsException;
14 import org.mockito.stubbing.OngoingStubbing;
15 
16 public abstract class BaseStubbing<T> implements OngoingStubbing<T> {
17 
18     @Override
thenReturn(T value)19     public OngoingStubbing<T> thenReturn(T value) {
20         return thenAnswer(new Returns(value));
21     }
22 
23     @Override
thenReturn(T value, T... values)24     public OngoingStubbing<T> thenReturn(T value, T... values) {
25         OngoingStubbing<T> stubbing = thenReturn(value);
26         if (values == null) {
27             // For no good reason we're configuring null answer here
28             // This has been like that since forever, so let's keep it for compatibility (unless users complain)
29             return stubbing.thenReturn(null);
30         }
31         for (T v : values) {
32             stubbing = stubbing.thenReturn(v);
33         }
34         return stubbing;
35     }
36 
thenThrow(Throwable throwable)37     private OngoingStubbing<T> thenThrow(Throwable throwable) {
38         return thenAnswer(new ThrowsException(throwable));
39     }
40 
41     @Override
thenThrow(Throwable... throwables)42     public OngoingStubbing<T> thenThrow(Throwable... throwables) {
43         if (throwables == null) {
44             return thenThrow((Throwable) null);
45         }
46         OngoingStubbing<T> stubbing = null;
47         for (Throwable t : throwables) {
48             if (stubbing == null) {
49                 stubbing = thenThrow(t);
50             } else {
51                 stubbing = stubbing.thenThrow(t);
52             }
53         }
54         return stubbing;
55     }
56 
57     @Override
thenThrow(Class<? extends Throwable> throwableType)58     public OngoingStubbing<T> thenThrow(Class<? extends Throwable> throwableType) {
59         if (throwableType == null) {
60             mockingProgress().reset();
61             throw notAnException();
62         }
63         return thenThrow(newInstance(throwableType));
64     }
65 
66     @Override
thenThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown)67     public OngoingStubbing<T> thenThrow(Class<? extends Throwable> toBeThrown, Class<? extends Throwable>... nextToBeThrown) {
68         if (nextToBeThrown == null) {
69             return thenThrow((Class<Throwable>) null);
70         }
71         OngoingStubbing<T> stubbing = thenThrow(toBeThrown);
72         for (Class<? extends Throwable> t : nextToBeThrown) {
73             stubbing = stubbing.thenThrow(t);
74         }
75         return stubbing;
76     }
77 
78     @Override
thenCallRealMethod()79     public OngoingStubbing<T> thenCallRealMethod() {
80         return thenAnswer(new CallsRealMethods());
81     }
82 }
83 
84 
85