Skip to content

Commit 7eeb04c

Browse files
committed
Adapt to Mockito support in the Test Context Framework
This commit updates MockitoTestExecutionListener to not handle mocks as this is already done in the listener provided by the core framework, and registration can only happen once. Integration tests have been left as-is to validate that the presence of both listeners don't have an unwanted side effect.
1 parent 97a771c commit 7eeb04c

File tree

2 files changed

+2
-76
lines changed

2 files changed

+2
-76
lines changed

spring-boot-project/spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoTestExecutionListener.java

+1-67
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,18 @@
1616

1717
package org.springframework.boot.test.mock.mockito;
1818

19-
import java.lang.annotation.Annotation;
2019
import java.lang.reflect.Field;
21-
import java.util.LinkedHashSet;
22-
import java.util.Set;
2320
import java.util.function.BiConsumer;
2421

25-
import org.mockito.Captor;
26-
import org.mockito.MockitoAnnotations;
27-
2822
import org.springframework.test.context.TestContext;
2923
import org.springframework.test.context.TestExecutionListener;
3024
import org.springframework.test.context.support.AbstractTestExecutionListener;
3125
import org.springframework.test.context.support.DependencyInjectionTestExecutionListener;
3226
import org.springframework.util.ReflectionUtils;
33-
import org.springframework.util.ReflectionUtils.FieldCallback;
3427

3528
/**
3629
* {@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.
4031
* <p>
4132
* To use the automatic reset support of {@code @MockBean} and {@code @SpyBean}, configure
4233
* {@link ResetMocksTestExecutionListener} as well.
@@ -49,59 +40,24 @@
4940
*/
5041
public class MockitoTestExecutionListener extends AbstractTestExecutionListener {
5142

52-
private static final String MOCKS_ATTRIBUTE_NAME = MockitoTestExecutionListener.class.getName() + ".mocks";
53-
5443
@Override
5544
public final int getOrder() {
5645
return 1950;
5746
}
5847

5948
@Override
6049
public void prepareTestInstance(TestContext testContext) throws Exception {
61-
closeMocks(testContext);
62-
initMocks(testContext);
6350
injectFields(testContext);
6451
}
6552

6653
@Override
6754
public void beforeTestMethod(TestContext testContext) throws Exception {
6855
if (Boolean.TRUE.equals(
6956
testContext.getAttribute(DependencyInjectionTestExecutionListener.REINJECT_DEPENDENCIES_ATTRIBUTE))) {
70-
closeMocks(testContext);
71-
initMocks(testContext);
7257
reinjectFields(testContext);
7358
}
7459
}
7560

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-
10561
private void injectFields(TestContext testContext) {
10662
postProcessFields(testContext, (mockitoField, postProcessor) -> postProcessor.inject(mockitoField.field,
10763
mockitoField.target, mockitoField.definition));
@@ -130,28 +86,6 @@ private void postProcessFields(TestContext testContext, BiConsumer<MockitoField,
13086
}
13187
}
13288

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-
15589
private static final class MockitoField {
15690

15791
private final Field field;

spring-boot-project/spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockitoTestExecutionListenerTests.java

+1-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2023 the original author or authors.
2+
* Copyright 2012-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -53,14 +53,6 @@ class MockitoTestExecutionListenerTests {
5353
@Mock
5454
private MockitoPostProcessor postProcessor;
5555

56-
@Test
57-
void prepareTestInstanceShouldInitMockitoAnnotations() throws Exception {
58-
WithMockitoAnnotations instance = new WithMockitoAnnotations();
59-
this.listener.prepareTestInstance(mockTestContext(instance));
60-
assertThat(instance.mock).isNotNull();
61-
assertThat(instance.captor).isNotNull();
62-
}
63-
6456
@Test
6557
void prepareTestInstanceShouldInjectMockBean() throws Exception {
6658
given(this.applicationContext.getBean(MockitoPostProcessor.class)).willReturn(this.postProcessor);

0 commit comments

Comments
 (0)