Skip to content

Commit fe6d914

Browse files
committed
Avoid hard dependency on Spring AOP for mock resolution
Closes gh-33774
1 parent c160e89 commit fe6d914

File tree

2 files changed

+18
-9
lines changed

2 files changed

+18
-9
lines changed

Diff for: spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/MockitoSpyBeanOverrideHandler.java

+5-2
Original file line numberDiff line numberDiff line change
@@ -70,19 +70,22 @@ protected Object createOverrideInstance(String beanName, @Nullable BeanDefinitio
7070
return createSpy(beanName, existingBeanInstance);
7171
}
7272

73-
@SuppressWarnings("unchecked")
7473
private Object createSpy(String name, Object instance) {
7574
Class<?> resolvedTypeToOverride = getBeanType().resolve();
7675
Assert.notNull(resolvedTypeToOverride, "Failed to resolve type to override");
7776
Assert.isInstanceOf(resolvedTypeToOverride, instance);
7877
if (Mockito.mockingDetails(instance).isSpy()) {
7978
return instance;
8079
}
80+
8181
MockSettings settings = MockReset.withSettings(getReset());
8282
if (StringUtils.hasLength(name)) {
8383
settings.name(name);
8484
}
85-
settings.verificationStartedListeners(verificationStartedListener);
85+
if (SpringMockResolver.aopAvailable) {
86+
settings.verificationStartedListeners(verificationStartedListener);
87+
}
88+
8689
Class<?> toSpy;
8790
if (Proxy.isProxyClass(instance.getClass())) {
8891
settings.defaultAnswer(AdditionalAnswers.delegatesTo(instance));

Diff for: spring-test/src/main/java/org/springframework/test/context/bean/override/mockito/SpringMockResolver.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.springframework.aop.framework.Advised;
2323
import org.springframework.aop.support.AopUtils;
2424
import org.springframework.util.Assert;
25+
import org.springframework.util.ClassUtils;
2526

2627
/**
2728
* A {@link MockResolver} for testing Spring applications with Mockito.
@@ -31,30 +32,35 @@
3132
*
3233
* @author Sam Brannen
3334
* @author Andy Wilkinson
35+
* @author Juergen Hoeller
3436
* @since 6.2
3537
*/
3638
public 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

Comments
 (0)