16
16
17
17
package org .springframework .test .context .bean .override .mockito ;
18
18
19
+ import java .lang .reflect .AnnotatedElement ;
19
20
import java .lang .reflect .Field ;
20
21
import java .util .Arrays ;
21
22
import java .util .HashSet ;
34
35
import org .springframework .core .Ordered ;
35
36
import org .springframework .core .annotation .MergedAnnotation ;
36
37
import org .springframework .core .annotation .MergedAnnotations ;
38
+ import org .springframework .core .annotation .MergedAnnotations .SearchStrategy ;
37
39
import org .springframework .lang .Nullable ;
38
40
import org .springframework .test .context .TestContext ;
39
41
import org .springframework .test .context .TestContextAnnotationUtils ;
@@ -57,10 +59,8 @@ public class MockitoResetTestExecutionListener extends AbstractTestExecutionList
57
59
58
60
private static final String SPRING_MOCKITO_PACKAGE = "org.springframework.test.context.bean.override.mockito" ;
59
61
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 );
64
64
65
65
/**
66
66
* Executes before {@link org.springframework.test.context.bean.override.BeanOverrideTestExecutionListener}.
@@ -153,13 +153,13 @@ static boolean hasMockitoAnnotations(TestContext testContext) {
153
153
*/
154
154
private static boolean hasMockitoAnnotations (Class <?> clazz ) {
155
155
// Declared on the class?
156
- if (MergedAnnotations . from (clazz , MergedAnnotations . SearchStrategy . DIRECT ). stream (). anyMatch ( isMockitoAnnotation )) {
156
+ if (isAnnotated (clazz )) {
157
157
return true ;
158
158
}
159
159
160
160
// Declared on a field?
161
161
for (Field field : clazz .getDeclaredFields ()) {
162
- if (MergedAnnotations . from (field , MergedAnnotations . SearchStrategy . DIRECT ). stream (). anyMatch ( isMockitoAnnotation )) {
162
+ if (isAnnotated (field )) {
163
163
return true ;
164
164
}
165
165
}
@@ -179,7 +179,7 @@ private static boolean hasMockitoAnnotations(Class<?> clazz) {
179
179
}
180
180
}
181
181
182
- // Declared on an enclosing class of an inner class ?
182
+ // Declared on an enclosing class?
183
183
if (TestContextAnnotationUtils .searchEnclosingClass (clazz )) {
184
184
if (hasMockitoAnnotations (clazz .getEnclosingClass ())) {
185
185
return true ;
@@ -189,4 +189,8 @@ private static boolean hasMockitoAnnotations(Class<?> clazz) {
189
189
return false ;
190
190
}
191
191
192
+ private static boolean isAnnotated (AnnotatedElement element ) {
193
+ return MergedAnnotations .from (element , SearchStrategy .DIRECT ).stream ().anyMatch (isSpringMockitoAnnotation );
194
+ }
195
+
192
196
}
0 commit comments