/
Mock.java
159 lines (142 loc) · 5.21 KB
/
Mock.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito;
import static java.lang.annotation.ElementType.FIELD;
import static java.lang.annotation.ElementType.PARAMETER;
import static java.lang.annotation.RetentionPolicy.RUNTIME;
import java.lang.annotation.Documented;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.plugins.MockMaker;
import org.mockito.stubbing.Answer;
/**
* Mark a field as a mock.
*
* <ul>
* <li>Allows shorthand mock creation.</li>
* <li>Minimizes repetitive mock creation code.</li>
* <li>Makes the test class more readable.</li>
* <li>Makes the verification error easier to read because the <b>field name</b> is used to identify the mock.</li>
* <li>Automatically detects static mocks of type {@link MockedStatic} and infers the static mock type of the type parameter.</li>
* </ul>
*
* <pre class="code"><code class="java">
* public class ArticleManagerTest extends SampleBaseTestCase {
*
* @Mock private ArticleCalculator calculator;
* @Mock(name = "database") private ArticleDatabase dbMock;
* @Mock(answer = RETURNS_MOCKS) private UserProvider userProvider;
* @Mock(extraInterfaces = {Queue.class, Observer.class}) private ArticleMonitor articleMonitor;
* @Mock(strictness = Mock.Strictness.LENIENT) private ArticleConsumer articleConsumer;
* @Mock(stubOnly = true) private Logger logger;
*
* private ArticleManager manager;
*
* @Before public void setup() {
* manager = new ArticleManager(userProvider, database, calculator, articleMonitor, articleConsumer, logger);
* }
* }
*
* public class SampleBaseTestCase {
*
* private AutoCloseable closeable;
*
* @Before public void openMocks() {
* closeable = MockitoAnnotations.openMocks(this);
* }
*
* @After public void releaseMocks() throws Exception {
* closeable.close();
* }
* }
* </code></pre>
*
* <p>
* <strong><code>MockitoAnnotations.openMocks(this)</code></strong> method has to be called to initialize annotated objects.
* In above example, <code>openMocks()</code> is called in @Before (JUnit4) method of test's base class.
* <strong>Instead</strong> you can also put openMocks() in your JUnit runner (@RunWith) or use the built-in
* {@link MockitoJUnitRunner}. Also, make sure to release any mocks after disposing your test class with a corresponding hook.
* </p>
*
* @see Mockito#mock(Class)
* @see Spy
* @see InjectMocks
* @see MockitoAnnotations#openMocks(Object)
* @see MockitoJUnitRunner
*/
@Target({FIELD, PARAMETER})
@Retention(RUNTIME)
@Documented
public @interface Mock {
/**
* Mock will have custom answer, see {@link MockSettings#defaultAnswer(Answer)}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*/
Answers answer() default Answers.RETURNS_DEFAULTS;
/**
* Mock will be 'stubOnly', see {@link MockSettings#stubOnly()}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*/
boolean stubOnly() default false;
/**
* Mock will have custom name (shown in verification errors), see {@link MockSettings#name(String)}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*/
String name() default "";
/**
* Mock will have extra interfaces, see {@link MockSettings#extraInterfaces(Class[])}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*/
Class<?>[] extraInterfaces() default {};
/**
* Mock will be serializable, see {@link MockSettings#serializable()}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*/
boolean serializable() default false;
/**
* @deprecated Use {@link Mock#strictness()} instead.
*
* Mock will be lenient, see {@link MockSettings#lenient()}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*
* @since 2.23.3
*/
@Deprecated
boolean lenient() default false;
/**
* Mock will have custom strictness, see {@link MockSettings#strictness(org.mockito.quality.Strictness)}.
* For examples how to use 'Mock' annotation and parameters see {@link Mock}.
*
* @since 4.6.1
*/
Strictness strictness() default Strictness.TEST_LEVEL_DEFAULT;
/**
* Mock will be created by the given {@link MockMaker}, see {@link MockSettings#mockMaker(String)}.
*
* @since 4.8.0
*/
String mockMaker() default "";
enum Strictness {
/**
* Default value used to indicate the mock does not override the test level strictness.
*
* @since 4.6.1
*/
TEST_LEVEL_DEFAULT,
/**
* See {@link org.mockito.quality.Strictness#LENIENT}
*/
LENIENT,
/**
* See {@link org.mockito.quality.Strictness#WARN}
*/
WARN,
/**
* See {@link org.mockito.quality.Strictness#STRICT_STUBS}
*/
STRICT_STUBS
}
}