Skip to content

Commit 8fa99dc

Browse files
committed
Polishing
1 parent 5bf179b commit 8fa99dc

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoResetTestExecutionListener.java

+11-7
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.test.context.bean.override.mockito;
1818

19+
import java.lang.reflect.AnnotatedElement;
1920
import java.lang.reflect.Field;
2021
import java.util.Arrays;
2122
import java.util.HashSet;
@@ -34,6 +35,7 @@
3435
import org.springframework.core.Ordered;
3536
import org.springframework.core.annotation.MergedAnnotation;
3637
import org.springframework.core.annotation.MergedAnnotations;
38+
import org.springframework.core.annotation.MergedAnnotations.SearchStrategy;
3739
import org.springframework.lang.Nullable;
3840
import org.springframework.test.context.TestContext;
3941
import org.springframework.test.context.TestContextAnnotationUtils;
@@ -57,10 +59,8 @@ public class MockitoResetTestExecutionListener extends AbstractTestExecutionList
5759

5860
private static final String SPRING_MOCKITO_PACKAGE = "org.springframework.test.context.bean.override.mockito";
5961

60-
private static final Predicate<MergedAnnotation<?>> isMockitoAnnotation = mergedAnnotation -> {
61-
String packageName = mergedAnnotation.getType().getPackageName();
62-
return packageName.startsWith(SPRING_MOCKITO_PACKAGE);
63-
};
62+
private static final Predicate<MergedAnnotation<?>> isSpringMockitoAnnotation = mergedAnnotation ->
63+
mergedAnnotation.getType().getPackageName().equals(SPRING_MOCKITO_PACKAGE);
6464

6565
/**
6666
* Executes before {@link org.springframework.test.context.bean.override.BeanOverrideTestExecutionListener}.
@@ -153,13 +153,13 @@ static boolean hasMockitoAnnotations(TestContext testContext) {
153153
*/
154154
private static boolean hasMockitoAnnotations(Class<?> clazz) {
155155
// Declared on the class?
156-
if (MergedAnnotations.from(clazz, MergedAnnotations.SearchStrategy.DIRECT).stream().anyMatch(isMockitoAnnotation)) {
156+
if (isAnnotated(clazz)) {
157157
return true;
158158
}
159159

160160
// Declared on a field?
161161
for (Field field : clazz.getDeclaredFields()) {
162-
if (MergedAnnotations.from(field, MergedAnnotations.SearchStrategy.DIRECT).stream().anyMatch(isMockitoAnnotation)) {
162+
if (isAnnotated(field)) {
163163
return true;
164164
}
165165
}
@@ -179,7 +179,7 @@ private static boolean hasMockitoAnnotations(Class<?> clazz) {
179179
}
180180
}
181181

182-
// Declared on an enclosing class of an inner class?
182+
// Declared on an enclosing class?
183183
if (TestContextAnnotationUtils.searchEnclosingClass(clazz)) {
184184
if (hasMockitoAnnotations(clazz.getEnclosingClass())) {
185185
return true;
@@ -189,4 +189,8 @@ private static boolean hasMockitoAnnotations(Class<?> clazz) {
189189
return false;
190190
}
191191

192+
private static boolean isAnnotated(AnnotatedElement element) {
193+
return MergedAnnotations.from(element, SearchStrategy.DIRECT).stream().anyMatch(isSpringMockitoAnnotation);
194+
}
195+
192196
}

0 commit comments

Comments
 (0)