/
MockitoExtension.java
228 lines (199 loc) · 7.88 KB
/
MockitoExtension.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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
/*
* Copyright (c) 2018 Mockito contributors
* This program is made available under the terms of the MIT License.
*/
package org.mockito.junit.jupiter;
import org.junit.jupiter.api.extension.*;
import org.junit.jupiter.api.extension.ExtensionContext.Namespace;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoSession;
import org.mockito.internal.configuration.plugins.Plugins;
import org.mockito.internal.session.MockitoSessionLoggerAdapter;
import org.mockito.internal.configuration.MockAnnotationProcessor;
import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.quality.Strictness;
import java.lang.reflect.Parameter;
import java.util.LinkedHashSet;
import java.util.Optional;
import java.util.Set;
import static org.junit.jupiter.api.extension.ExtensionContext.Namespace.create;
import static org.junit.platform.commons.support.AnnotationSupport.findAnnotation;
/**
* Extension that initializes mocks and handles strict stubbings. This extension is the JUnit Jupiter equivalent
* of our JUnit4 {@link MockitoJUnitRunner}.
*
* Example usage:
*
* <pre class="code"><code class="java">
* <b>@ExtendWith(MockitoExtension.class)</b>
* public class ExampleTest {
*
* @Mock
* private List<Integer> list;
*
* @Test
* public void shouldDoSomething() {
* list.add(100);
* }
* }
* </code></pre>
*
* If you would like to configure the used strictness for the test class, use {@link MockitoSettings}.
*
* <pre class="code"><code class="java">
* <b>@MockitoSettings(strictness = Strictness.STRICT_STUBS)</b>
* public class ExampleTest {
*
* @Mock
* private List<Integer> list;
*
* @Test
* public void shouldDoSomething() {
* list.add(100);
* }
* }
* </code></pre>
*
* This extension also supports JUnit Jupiter's method parameters.
* Use parameters for initialization of mocks that you use only in that specific test method.
* In other words, where you would initialize local mocks in JUnit 4 by calling {@link Mockito#mock(Class)},
* use the method parameter. This is especially beneficial when initializing a mock with generics, as you no
* longer get a warning about "Unchecked assignment".
* Please refer to JUnit Jupiter's documentation to learn when method parameters are useful.
*
* <pre class="code"><code class="java">
* <b>@ExtendWith(MockitoExtension.class)</b>
* public class ExampleTest {
*
* @Mock
* private List<Integer> sharedList;
*
* @Test
* public void shouldDoSomething() {
* sharedList.add(100);
* }
*
* @Test
* public void hasLocalMockInThisTest(@Mock List<Integer> localList) {
* localList.add(100);
* sharedList.add(100);
* }
* }
* </code></pre>
*
* Lastly, the extension supports JUnit Jupiter's constructor parameters.
* This allows you to do setup work in the constructor and set
* your fields to <code>final</code>.
* Please refer to JUnit Jupiter's documentation to learn when constructor parameters are useful.
*
* <pre class="code"><code class="java">
* <b>@ExtendWith(MockitoExtension.class)</b>
* public class ExampleTest {
*
* private final List<Integer> sharedList;
*
* ExampleTest(@Mock sharedList) {
* this.sharedList = sharedList;
* }
*
* @Test
* public void shouldDoSomething() {
* sharedList.add(100);
* }
* }
* </code></pre>
*/
public class MockitoExtension implements TestInstancePostProcessor, BeforeEachCallback, AfterEachCallback, ParameterResolver {
private final static Namespace MOCKITO = create("org.mockito");
private final static String SESSION = "session";
private final static String TEST_INSTANCE = "testInstance";
private final Strictness strictness;
// This constructor is invoked by JUnit Jupiter via reflection or ServiceLoader
@SuppressWarnings("unused")
public MockitoExtension() {
this(Strictness.STRICT_STUBS);
}
private MockitoExtension(Strictness strictness) {
this.strictness = strictness;
}
/**
* <p>Callback for post-processing the supplied test instance.
*
* <p><strong>Note</strong>: the {@code ExtensionContext} supplied to a
* {@code TestInstancePostProcessor} will always return an empty
* {@link Optional} value from {@link ExtensionContext#getTestInstance()
* getTestInstance()}. A {@code TestInstancePostProcessor} should therefore
* only attempt to process the supplied {@code testInstance}.
*
* @param testInstance the instance to post-process; never {@code null}
* @param context the current extension context; never {@code null}
*/
@Override
public void postProcessTestInstance(Object testInstance, ExtensionContext context) {
context.getStore(MOCKITO).put(TEST_INSTANCE, testInstance);
}
/**
* Callback that is invoked <em>before</em> each test is invoked.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void beforeEach(final ExtensionContext context) {
Set<Object> testInstances = new LinkedHashSet<>();
testInstances.add(context.getRequiredTestInstance());
this.collectParentTestInstances(context, testInstances);
Strictness actualStrictness = this.retrieveAnnotationFromTestClasses(context)
.map(MockitoSettings::strictness)
.orElse(strictness);
MockitoSession session = Mockito.mockitoSession()
.initMocks(testInstances.toArray())
.strictness(actualStrictness)
.logger(new MockitoSessionLoggerAdapter(Plugins.getMockitoLogger()))
.startMocking();
context.getStore(MOCKITO).put(SESSION, session);
}
private Optional<MockitoSettings> retrieveAnnotationFromTestClasses(final ExtensionContext context) {
ExtensionContext currentContext = context;
Optional<MockitoSettings> annotation;
do {
annotation = findAnnotation(currentContext.getElement(), MockitoSettings.class);
if (!currentContext.getParent().isPresent()) {
break;
}
currentContext = currentContext.getParent().get();
} while (!annotation.isPresent() && currentContext != context.getRoot());
return annotation;
}
private void collectParentTestInstances(ExtensionContext context, Set<Object> testInstances) {
Optional<ExtensionContext> parent = context.getParent();
while (parent.isPresent() && parent.get() != context.getRoot()) {
ExtensionContext parentContext = parent.get();
Object testInstance = parentContext.getStore(MOCKITO).remove(TEST_INSTANCE);
if (testInstance != null) {
testInstances.add(testInstance);
}
parent = parentContext.getParent();
}
}
/**
* Callback that is invoked <em>after</em> each test has been invoked.
*
* @param context the current extension context; never {@code null}
*/
@Override
public void afterEach(ExtensionContext context) {
context.getStore(MOCKITO).remove(SESSION, MockitoSession.class)
.finishMocking();
}
@Override
public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
return parameterContext.isAnnotated(Mock.class);
}
@SuppressWarnings("ConstantConditions")
@Override
public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {
final Parameter parameter = parameterContext.getParameter();
return MockAnnotationProcessor.processAnnotationForMock(parameterContext.findAnnotation(Mock.class).get(), parameter.getType(), parameter.getName());
}
}