|
16 | 16 |
|
17 | 17 | package org.springframework.boot.test.mock.mockito;
|
18 | 18 |
|
19 |
| -import java.lang.annotation.Annotation; |
20 | 19 | import java.lang.reflect.Field;
|
21 |
| -import java.util.LinkedHashSet; |
22 |
| -import java.util.Set; |
23 | 20 | import java.util.function.BiConsumer;
|
24 | 21 |
|
25 |
| -import org.mockito.Captor; |
26 |
| -import org.mockito.MockitoAnnotations; |
27 |
| - |
28 | 22 | import org.springframework.test.context.TestContext;
|
29 | 23 | import org.springframework.test.context.TestExecutionListener;
|
30 | 24 | import org.springframework.test.context.support.AbstractTestExecutionListener;
|
31 | 25 | import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
|
32 | 26 | import org.springframework.util.ReflectionUtils;
|
33 |
| -import org.springframework.util.ReflectionUtils.FieldCallback; |
34 | 27 |
|
35 | 28 | /**
|
36 | 29 | * {@link TestExecutionListener} to enable {@link MockBean @MockBean} and
|
37 |
| - * {@link SpyBean @SpyBean} support. Also triggers |
38 |
| - * {@link MockitoAnnotations#openMocks(Object)} when any Mockito annotations used, |
39 |
| - * primarily to allow {@link Captor @Captor} annotations. |
| 30 | + * {@link SpyBean @SpyBean} support. |
40 | 31 | * <p>
|
41 | 32 | * To use the automatic reset support of {@code @MockBean} and {@code @SpyBean}, configure
|
42 | 33 | * {@link ResetMocksTestExecutionListener} as well.
|
|
49 | 40 | */
|
50 | 41 | public class MockitoTestExecutionListener extends AbstractTestExecutionListener {
|
51 | 42 |
|
52 |
| - private static final String MOCKS_ATTRIBUTE_NAME = MockitoTestExecutionListener.class.getName() + ".mocks"; |
53 |
| - |
54 | 43 | @Override
|
55 | 44 | public final int getOrder() {
|
56 | 45 | return 1950;
|
57 | 46 | }
|
58 | 47 |
|
59 | 48 | @Override
|
60 | 49 | public void prepareTestInstance(TestContext testContext) throws Exception {
|
61 |
| - closeMocks(testContext); |
62 |
| - initMocks(testContext); |
63 | 50 | injectFields(testContext);
|
64 | 51 | }
|
65 | 52 |
|
66 | 53 | @Override
|
67 | 54 | public void beforeTestMethod(TestContext testContext) throws Exception {
|
68 | 55 | if (Boolean.TRUE.equals(
|
69 | 56 | testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {
|
70 |
| - closeMocks(testContext); |
71 |
| - initMocks(testContext); |
72 | 57 | reinjectFields(testContext);
|
73 | 58 | }
|
74 | 59 | }
|
75 | 60 |
|
76 |
| - @Override |
77 |
| - public void afterTestMethod(TestContext testContext) throws Exception { |
78 |
| - closeMocks(testContext); |
79 |
| - } |
80 |
| - |
81 |
| - @Override |
82 |
| - public void afterTestClass(TestContext testContext) throws Exception { |
83 |
| - closeMocks(testContext); |
84 |
| - } |
85 |
| - |
86 |
| - private void initMocks(TestContext testContext) { |
87 |
| - if (hasMockitoAnnotations(testContext)) { |
88 |
| - testContext.setAttribute(MOCKS_ATTRIBUTE_NAME, MockitoAnnotations.openMocks(testContext.getTestInstance())); |
89 |
| - } |
90 |
| - } |
91 |
| - |
92 |
| - private void closeMocks(TestContext testContext) throws Exception { |
93 |
| - Object mocks = testContext.getAttribute(MOCKS_ATTRIBUTE_NAME); |
94 |
| - if (mocks instanceof AutoCloseable closeable) { |
95 |
| - closeable.close(); |
96 |
| - } |
97 |
| - } |
98 |
| - |
99 |
| - private boolean hasMockitoAnnotations(TestContext testContext) { |
100 |
| - MockitoAnnotationCollection collector = new MockitoAnnotationCollection(); |
101 |
| - ReflectionUtils.doWithFields(testContext.getTestClass(), collector); |
102 |
| - return collector.hasAnnotations(); |
103 |
| - } |
104 |
| - |
105 | 61 | private void injectFields(TestContext testContext) {
|
106 | 62 | postProcessFields(testContext, (mockitoField, postProcessor) -> postProcessor.inject(mockitoField.field,
|
107 | 63 | mockitoField.target, mockitoField.definition));
|
@@ -130,28 +86,6 @@ private void postProcessFields(TestContext testContext, BiConsumer<MockitoField,
|
130 | 86 | }
|
131 | 87 | }
|
132 | 88 |
|
133 |
| - /** |
134 |
| - * {@link FieldCallback} to collect Mockito annotations. |
135 |
| - */ |
136 |
| - private static final class MockitoAnnotationCollection implements FieldCallback { |
137 |
| - |
138 |
| - private final Set<Annotation> annotations = new LinkedHashSet<>(); |
139 |
| - |
140 |
| - @Override |
141 |
| - public void doWith(Field field) throws IllegalArgumentException { |
142 |
| - for (Annotation annotation : field.getDeclaredAnnotations()) { |
143 |
| - if (annotation.annotationType().getName().startsWith("org.mockito")) { |
144 |
| - this.annotations.add(annotation); |
145 |
| - } |
146 |
| - } |
147 |
| - } |
148 |
| - |
149 |
| - boolean hasAnnotations() { |
150 |
| - return !this.annotations.isEmpty(); |
151 |
| - } |
152 |
| - |
153 |
| - } |
154 |
| - |
155 | 89 | private static final class MockitoField {
|
156 | 90 |
|
157 | 91 | private final Field field;
|
|
0 commit comments