22
22
import org .springframework .aop .framework .Advised ;
23
23
import org .springframework .aop .support .AopUtils ;
24
24
import org .springframework .util .Assert ;
25
+ import org .springframework .util .ClassUtils ;
25
26
26
27
/**
27
28
* A {@link MockResolver} for testing Spring applications with Mockito.
31
32
*
32
33
* @author Sam Brannen
33
34
* @author Andy Wilkinson
35
+ * @author Juergen Hoeller
34
36
* @since 6.2
35
37
*/
36
38
public class SpringMockResolver implements MockResolver {
37
39
40
+ static final boolean aopAvailable = ClassUtils .isPresent (
41
+ "org.springframework.aop.framework.Advised" , SpringMockResolver .class .getClassLoader ());
42
+
43
+
38
44
@ Override
39
45
public Object resolve (Object instance ) {
40
- return getUltimateTargetObject (instance );
46
+ if (aopAvailable ) {
47
+ return getUltimateTargetObject (instance );
48
+ }
49
+ return instance ;
41
50
}
42
51
43
52
/**
44
53
* This is a modified version of
45
54
* {@link org.springframework.test.util.AopTestUtils#getUltimateTargetObject(Object)
46
- * AopTestUtils#getUltimateTargetObject()} which only checks static target
47
- * sources.
48
- * @param <T> the type of the target object
55
+ * AopTestUtils#getUltimateTargetObject()} which only checks static target sources.
49
56
* @param candidate the instance to check (potentially a Spring AOP proxy;
50
57
* never {@code null})
51
58
* @return the target object or the {@code candidate} (never {@code null})
52
59
* @throws IllegalStateException if an error occurs while unwrapping a proxy
53
60
* @see Advised#getTargetSource()
54
61
* @see TargetSource#isStatic()
55
62
*/
56
- @ SuppressWarnings ("unchecked" )
57
- static <T > T getUltimateTargetObject (Object candidate ) {
63
+ static Object getUltimateTargetObject (Object candidate ) {
58
64
Assert .notNull (candidate , "Candidate must not be null" );
59
65
try {
60
66
if (AopUtils .isAopProxy (candidate ) && candidate instanceof Advised advised ) {
@@ -70,7 +76,7 @@ static <T> T getUltimateTargetObject(Object candidate) {
70
76
catch (Throwable ex ) {
71
77
throw new IllegalStateException ("Failed to unwrap proxied object" , ex );
72
78
}
73
- return ( T ) candidate ;
79
+ return candidate ;
74
80
}
75
81
76
82
}
0 commit comments