• 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 package org.mockito;
6 
7 import org.mockito.internal.stubbing.answers.CallsRealMethods;
8 import org.mockito.internal.stubbing.defaultanswers.TriesToReturnSelf;
9 import org.mockito.internal.stubbing.defaultanswers.GloballyConfiguredAnswer;
10 import org.mockito.internal.stubbing.defaultanswers.ReturnsDeepStubs;
11 import org.mockito.internal.stubbing.defaultanswers.ReturnsMocks;
12 import org.mockito.internal.stubbing.defaultanswers.ReturnsSmartNulls;
13 import org.mockito.invocation.InvocationOnMock;
14 import org.mockito.stubbing.Answer;
15 
16 /**
17  * Enumeration of pre-configured mock answers
18  * <p>
19  * You can use it to pass extra parameters to &#064;Mock annotation, see more info here: {@link Mock}
20  * <p>
21  * Example:
22  * <pre class="code"><code class="java">
23  *   &#064;Mock(answer = RETURNS_DEEP_STUBS) UserProvider userProvider;
24  * </code></pre>
25  * <b>This is not the full list</b> of Answers available in Mockito. Some interesting answers can be found in org.mockito.stubbing.answers package.
26  */
27 public enum Answers implements Answer<Object>{
28     /**
29      * The default configured answer of every mock.
30      *
31      * <p>Please see the {@link org.mockito.Mockito#RETURNS_DEFAULTS} documentation for more details.</p>
32      *
33      * @see org.mockito.Mockito#RETURNS_DEFAULTS
34      */
35     RETURNS_DEFAULTS(new GloballyConfiguredAnswer()),
36 
37     /**
38      * An answer that returns smart-nulls.
39      *
40      * <p>Please see the {@link org.mockito.Mockito#RETURNS_SMART_NULLS} documentation for more details.</p>
41      *
42      * @see org.mockito.Mockito#RETURNS_SMART_NULLS
43      */
44     RETURNS_SMART_NULLS(new ReturnsSmartNulls()),
45 
46     /**
47      * An answer that returns <strong>mocks</strong> (not stubs).
48      *
49      * <p>Please see the {@link org.mockito.Mockito#RETURNS_MOCKS} documentation for more details.</p>
50      *
51      * @see org.mockito.Mockito#RETURNS_MOCKS
52      */
53     RETURNS_MOCKS(new ReturnsMocks()),
54 
55 
56     /**
57      * An answer that returns <strong>deep stubs</strong> (not mocks).
58      *
59      * <p>Please see the {@link org.mockito.Mockito#RETURNS_DEEP_STUBS} documentation for more details.</p>
60      *
61      * @see org.mockito.Mockito#RETURNS_DEEP_STUBS
62      */
63     RETURNS_DEEP_STUBS(new ReturnsDeepStubs()),
64 
65     /**
66      * An answer that calls the real methods (used for partial mocks).
67      *
68      * <p>Please see the {@link org.mockito.Mockito#CALLS_REAL_METHODS} documentation for more details.</p>
69      *
70      * @see org.mockito.Mockito#CALLS_REAL_METHODS
71      */
72     CALLS_REAL_METHODS(new CallsRealMethods()),
73 
74     /**
75      * An answer that tries to return itself. This is useful for mocking {@code Builders}.
76      *
77      * <p>Please see the {@link org.mockito.Mockito#RETURNS_SELF} documentation for more details.</p>
78      *
79      * @see org.mockito.Mockito#RETURNS_SELF
80      */
81     RETURNS_SELF(new TriesToReturnSelf())
82     ;
83 
84     private final Answer<Object> implementation;
85 
Answers(Answer<Object> implementation)86     Answers(Answer<Object> implementation) {
87         this.implementation = implementation;
88     }
89 
90     /**
91      * @deprecated as of 2.1.0 Use the enum-constant directly, instead of this getter. This method will be removed in a future release<br>
92      * E.g. instead of <code>Answers.CALLS_REAL_METHODS.get()</code> use <code>Answers.CALLS_REAL_METHODS</code> .
93      */
94     @Deprecated
get()95     public Answer<Object> get() {
96         return this;
97     }
98 
answer(InvocationOnMock invocation)99     public Object answer(InvocationOnMock invocation) throws Throwable {
100         return implementation.answer(invocation);
101     }
102 }
103