Skip to content

Commit d8a6423

Browse files
committed
Support transparent verification for @⁠MockitoSpyBean
Prior to this commit, SpringAopBypassingVerificationStartedListener provided partial support for transparent verification for Mockito spies created via @⁠MockitoSpyBean when the spy is wrapped in a Spring AOP proxy. However, attempting to actually verify invocations for a spy resulted in an exception from Mockito since MockUtil.isMock() returned false in such scenarios. This commit addresses that by introducing a SpringMockResolver that resolves mocks by walking the Spring AOP proxy chain until the target or a non-static proxy is found. SpringMockResolver is automatically registered whenever the spring-test JAR is on the classpath, allowing Mockito to transparently resolve mocks wrapped in Spring AOP proxies. Closes gh-33774
1 parent c85689b commit d8a6423

File tree

6 files changed

+203
-9
lines changed

6 files changed

+203
-9
lines changed

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import org.springframework.lang.Nullable;
3131
import org.springframework.test.context.bean.override.BeanOverrideHandler;
3232
import org.springframework.test.context.bean.override.BeanOverrideStrategy;
33-
import org.springframework.test.util.AopTestUtils;
3433
import org.springframework.util.Assert;
3534
import org.springframework.util.StringUtils;
3635

@@ -103,7 +102,7 @@ private static final class SpringAopBypassingVerificationStartedListener impleme
103102

104103
@Override
105104
public void onVerificationStarted(VerificationStartedEvent event) {
106-
event.setMock(AopTestUtils.getUltimateTargetObject(event.getMock()));
105+
event.setMock(SpringMockResolver.getUltimateTargetObject(event.getMock()));
107106
}
108107
}
109108

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2012-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.bean.override.mockito;
18+
19+
import org.mockito.plugins.MockResolver;
20+
21+
import org.springframework.aop.TargetSource;
22+
import org.springframework.aop.framework.Advised;
23+
import org.springframework.aop.support.AopUtils;
24+
import org.springframework.util.Assert;
25+
26+
/**
27+
* A {@link MockResolver} for testing Spring applications with Mockito.
28+
*
29+
* <p>Resolves mocks by walking the Spring AOP proxy chain until the target or a
30+
* non-static proxy is found.
31+
*
32+
* @author Sam Brannen
33+
* @author Andy Wilkinson
34+
* @since 6.2
35+
*/
36+
public class SpringMockResolver implements MockResolver {
37+
38+
@Override
39+
public Object resolve(Object instance) {
40+
return getUltimateTargetObject(instance);
41+
}
42+
43+
/**
44+
* This is a modified version of
45+
* {@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
49+
* @param candidate the instance to check (potentially a Spring AOP proxy;
50+
* never {@code null})
51+
* @return the target object or the {@code candidate} (never {@code null})
52+
* @throws IllegalStateException if an error occurs while unwrapping a proxy
53+
* @see Advised#getTargetSource()
54+
* @see TargetSource#isStatic()
55+
*/
56+
@SuppressWarnings("unchecked")
57+
static <T> T getUltimateTargetObject(Object candidate) {
58+
Assert.notNull(candidate, "Candidate must not be null");
59+
try {
60+
if (AopUtils.isAopProxy(candidate) && candidate instanceof Advised advised) {
61+
TargetSource targetSource = advised.getTargetSource();
62+
if (targetSource.isStatic()) {
63+
Object target = targetSource.getTarget();
64+
if (target != null) {
65+
return getUltimateTargetObject(target);
66+
}
67+
}
68+
}
69+
}
70+
catch (Throwable ex) {
71+
throw new IllegalStateException("Failed to unwrap proxied object", ex);
72+
}
73+
return (T) candidate;
74+
}
75+
76+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.test.context.bean.override.mockito.SpringMockResolver

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

+26-7
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@
1616

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

19+
import org.junit.jupiter.api.BeforeEach;
1920
import org.junit.jupiter.api.Disabled;
20-
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.RepeatedTest;
2122
import org.junit.jupiter.api.extension.ExtendWith;
2223
import org.mockito.Mockito;
2324

2425
import org.springframework.aop.support.AopUtils;
2526
import org.springframework.cache.CacheManager;
27+
import org.springframework.cache.annotation.CacheEvict;
2628
import org.springframework.cache.annotation.Cacheable;
2729
import org.springframework.cache.annotation.EnableCaching;
2830
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
@@ -58,12 +60,21 @@ class MockitoSpyBeanAndSpringAopProxyTests {
5860
DateService dateService;
5961

6062

63+
@BeforeEach
64+
void resetCache() {
65+
// We have to clear the "test" cache before each test. Otherwise, method
66+
// invocations on the Spring AOP proxy will never make it to the Mockito spy.
67+
dateService.clearCache();
68+
}
69+
6170
/**
6271
* Stubbing and verification for a Mockito spy that is wrapped in a Spring AOP
6372
* proxy should always work when performed via the ultimate target of the Spring
6473
* AOP proxy (i.e., the actual spy instance).
6574
*/
66-
@Test
75+
// We need to run this test at least twice to ensure the Mockito spy can be reused
76+
// across test method invocations without using @DirtestContext.
77+
@RepeatedTest(2)
6778
void stubAndVerifyOnUltimateTargetOfSpringAopProxy() {
6879
assertThat(AopUtils.isAopProxy(dateService)).as("is Spring AOP proxy").isTrue();
6980
DateService spy = AopTestUtils.getUltimateTargetObject(dateService);
@@ -93,13 +104,14 @@ void stubAndVerifyOnUltimateTargetOfSpringAopProxy() {
93104
* AOP proxy to stubbing calls, while supplying the Spring AOP proxy to verification
94105
* calls.
95106
*/
96-
@Disabled("Disabled until transparent verification for @MockitoSpyBean is implemented")
97-
@Test
107+
// We need to run this test at least twice to ensure the Mockito spy can be reused
108+
// across test method invocations without using @DirtestContext.
109+
@RepeatedTest(2)
98110
void stubOnUltimateTargetAndVerifyOnSpringAopProxy() {
99111
assertThat(AopUtils.isAopProxy(dateService)).as("is Spring AOP proxy").isTrue();
100-
DateService spy = AopTestUtils.getUltimateTargetObject(dateService);
101-
assertThat(Mockito.mockingDetails(spy).isSpy()).as("ultimate target is Mockito spy").isTrue();
112+
assertThat(Mockito.mockingDetails(dateService).isSpy()).as("Spring AOP proxy is Mockito spy").isTrue();
102113

114+
DateService spy = AopTestUtils.getUltimateTargetObject(dateService);
103115
given(spy.getDate(false)).willReturn(1L);
104116
Long date = dateService.getDate(false);
105117
assertThat(date).isOne();
@@ -123,7 +135,9 @@ void stubOnUltimateTargetAndVerifyOnSpringAopProxy() {
123135
* stubbing for a proxied mock.
124136
*/
125137
@Disabled("Disabled until Mockito provides support for transparent stubbing of a proxied spy")
126-
@Test
138+
// We need to run this test at least twice to ensure the Mockito spy can be reused
139+
// across test method invocations without using @DirtestContext.
140+
@RepeatedTest(2)
127141
void stubAndVerifyDirectlyOnSpringAopProxy() throws Exception {
128142
assertThat(AopUtils.isCglibProxy(dateService)).as("is Spring AOP CGLIB proxy").isTrue();
129143
assertThat(Mockito.mockingDetails(dateService).isSpy()).as("is Mockito spy").isTrue();
@@ -166,6 +180,11 @@ static class DateService {
166180
Long getDate(boolean argument) {
167181
return System.nanoTime();
168182
}
183+
184+
@CacheEvict(cacheNames = "test", allEntries = true)
185+
void clearCache() {
186+
}
187+
169188
}
170189

171190
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.bean.override.mockito;
18+
19+
import org.junit.jupiter.api.Test;
20+
import org.mockito.internal.configuration.plugins.Plugins;
21+
22+
import static org.assertj.core.api.Assertions.assertThat;
23+
24+
/**
25+
* Integration tests for {@link SpringMockResolver}.
26+
*
27+
* @author Andy Wilkinson
28+
* @since 6.2
29+
* @see SpringMockResolverTests
30+
*/
31+
class SpringMockResolverIntegrationTests {
32+
33+
@Test
34+
void customMockResolverIsRegisteredWithMockito() {
35+
assertThat(Plugins.getMockResolvers()).hasOnlyElementsOfType(SpringMockResolver.class);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2002-2024 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.test.context.bean.override.mockito;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aop.SpringProxy;
22+
import org.springframework.aop.framework.ProxyFactory;
23+
import org.springframework.aop.target.HotSwappableTargetSource;
24+
import org.springframework.aop.target.SingletonTargetSource;
25+
26+
import static org.assertj.core.api.Assertions.assertThat;
27+
28+
/**
29+
* Unit tests for {@link SpringMockResolver}.
30+
*
31+
* @author Moritz Halbritter
32+
* @author Sam Brannen
33+
* @since 6.2
34+
* @see SpringMockResolverIntegrationTests
35+
*/
36+
class SpringMockResolverTests {
37+
38+
@Test
39+
void staticTarget() {
40+
MyServiceImpl myService = new MyServiceImpl();
41+
MyService proxy = ProxyFactory.getProxy(MyService.class, new SingletonTargetSource(myService));
42+
Object target = new SpringMockResolver().resolve(proxy);
43+
assertThat(target).isInstanceOf(MyServiceImpl.class);
44+
}
45+
46+
@Test
47+
void nonStaticTarget() {
48+
MyServiceImpl myService = new MyServiceImpl();
49+
MyService proxy = ProxyFactory.getProxy(MyService.class, new HotSwappableTargetSource(myService));
50+
Object target = new SpringMockResolver().resolve(proxy);
51+
assertThat(target).isInstanceOf(SpringProxy.class);
52+
}
53+
54+
55+
private interface MyService {
56+
}
57+
58+
private static final class MyServiceImpl implements MyService {
59+
}
60+
61+
}

0 commit comments

Comments
 (0)