/
MockitoAnnotations.java
79 lines (75 loc) · 3.07 KB
/
MockitoAnnotations.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
/*
* Copyright (c) 2007 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito;
import org.mockito.exceptions.base.MockitoException;
import org.mockito.internal.configuration.GlobalConfiguration;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.plugins.AnnotationEngine;
/**
* MockitoAnnotations.initMocks(this); initializes fields annotated with Mockito annotations.
* See also {@link MockitoSession} which not only initializes mocks
* but also adds extra validation for cleaner tests!
* <p>
* <ul>
* <li>Allows shorthand creation of objects required for testing.</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 <b>field name</b> is used to identify the mock.</li>
* </ul>
*
* <pre class="code"><code class="java">
* public class ArticleManagerTest extends SampleBaseTestCase {
*
* @Mock private ArticleCalculator calculator;
* @Mock private ArticleDatabase database;
* @Mock private UserProvider userProvider;
*
* private ArticleManager manager;
*
* @Before public void setup() {
* manager = new ArticleManager(userProvider, database, calculator);
* }
* }
*
* public class SampleBaseTestCase {
*
* private AutoCloseable closeable;
*
* @Before public void initMocks() {
* closeable = MockitoAnnotations.initMocks(this);
* }
*
* @After public void releaseMocks() throws Exception {
* closeable.close();
* }
* }
* </code></pre>
* <p>
* Read also about other annotations @{@link Spy}, @{@link Captor}, @{@link InjectMocks}
* <p>
* <b><code>MockitoAnnotations.initMocks(this)</code></b> method has to be called to initialize annotated fields.
* <p>
* In above example, <code>initMocks()</code> is called in @Before (JUnit4) method of test's base class.
* For JUnit3 <code>initMocks()</code> can go to <code>setup()</code> method of a base class.
* You can also put initMocks() in your JUnit runner (@RunWith) or use built-in runner: {@link MockitoJUnitRunner}.
* If static method mocks are used, it is required to close the initialization.
*/
public class MockitoAnnotations {
/**
* Initializes objects annotated with Mockito annotations for given testClass:
* @{@link org.mockito.Mock}, @{@link Spy}, @{@link Captor}, @{@link InjectMocks}
* <p>
* See examples in javadoc for {@link MockitoAnnotations} class.
*/
public static AutoCloseable initMocks(Object testClass) {
if (testClass == null) {
throw new MockitoException(
"testClass cannot be null. For info how to use @Mock annotations see examples in javadoc for MockitoAnnotations class");
}
AnnotationEngine annotationEngine =
new GlobalConfiguration().tryGetPluginAnnotationEngine();
return annotationEngine.process(testClass.getClass(), testClass);
}
}