2222import org .springframework .aop .framework .Advised ;
2323import org .springframework .aop .support .AopUtils ;
2424import org .springframework .util .Assert ;
25+ import org .springframework .util .ClassUtils ;
2526
2627/**
2728 * A {@link MockResolver} for testing Spring applications with Mockito.
3132 *
3233 * @author Sam Brannen
3334 * @author Andy Wilkinson
35+ * @author Juergen Hoeller
3436 * @since 6.2
3537 */
3638public class SpringMockResolver implements MockResolver {
3739
40+ static final boolean aopAvailable = ClassUtils .isPresent (
41+ "org.springframework.aop.framework.Advised" , SpringMockResolver .class .getClassLoader ());
42+
43+
3844 @ Override
3945 public Object resolve (Object instance ) {
40- return getUltimateTargetObject (instance );
46+ if (aopAvailable ) {
47+ return getUltimateTargetObject (instance );
48+ }
49+ return instance ;
4150 }
4251
4352 /**
4453 * This is a modified version of
4554 * {@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.
4956 * @param candidate the instance to check (potentially a Spring AOP proxy;
5057 * never {@code null})
5158 * @return the target object or the {@code candidate} (never {@code null})
5259 * @throws IllegalStateException if an error occurs while unwrapping a proxy
5360 * @see Advised#getTargetSource()
5461 * @see TargetSource#isStatic()
5562 */
56- @ SuppressWarnings ("unchecked" )
57- static <T > T getUltimateTargetObject (Object candidate ) {
63+ static Object getUltimateTargetObject (Object candidate ) {
5864 Assert .notNull (candidate , "Candidate must not be null" );
5965 try {
6066 if (AopUtils .isAopProxy (candidate ) && candidate instanceof Advised advised ) {
@@ -70,7 +76,7 @@ static <T> T getUltimateTargetObject(Object candidate) {
7076 catch (Throwable ex ) {
7177 throw new IllegalStateException ("Failed to unwrap proxied object" , ex );
7278 }
73- return ( T ) candidate ;
79+ return candidate ;
7480 }
7581
7682}
0 commit comments