Skip to content

Commit 982f7f8

Browse files
committed
Verify @⁠MockitoBean & @⁠MockitoSpyBean can be used Spring AOP proxies
Use cases not yet supported with @⁠MockitoSpyBean are currently @⁠Disabled. See gh-33742
1 parent d05f880 commit 982f7f8

File tree

2 files changed

+283
-0
lines changed

2 files changed

+283
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
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.junit.jupiter.api.RepeatedTest;
20+
import org.junit.jupiter.api.extension.ExtendWith;
21+
import org.mockito.Mockito;
22+
23+
import org.springframework.aop.support.AopUtils;
24+
import org.springframework.cache.CacheManager;
25+
import org.springframework.cache.annotation.Cacheable;
26+
import org.springframework.cache.annotation.EnableCaching;
27+
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
28+
import org.springframework.cache.interceptor.CacheResolver;
29+
import org.springframework.cache.interceptor.SimpleCacheResolver;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.context.annotation.Import;
33+
import org.springframework.stereotype.Service;
34+
import org.springframework.test.context.junit.jupiter.SpringExtension;
35+
36+
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.mockito.ArgumentMatchers.anyBoolean;
38+
import static org.mockito.ArgumentMatchers.eq;
39+
import static org.mockito.BDDMockito.given;
40+
import static org.mockito.Mockito.times;
41+
import static org.mockito.Mockito.verify;
42+
43+
/**
44+
* Tests for {@link MockitoBean @MockitoBean} used in combination with Spring AOP.
45+
*
46+
* @author Phillip Webb
47+
* @author Sam Brannen
48+
* @since 6.2
49+
* @see <a href="https://github.com/spring-projects/spring-boot/issues/5837">5837</a>
50+
* @see MockitoSpyBeanAndAopProxyTests
51+
*/
52+
@ExtendWith(SpringExtension.class)
53+
class MockitoBeanAndAopProxyTests {
54+
55+
@MockitoBean
56+
DateService dateService;
57+
58+
59+
/**
60+
* Since we always register a manual singleton for {@code @MockitoBean} mocks,
61+
* Spring AOP should never be applied to the mock.
62+
*
63+
* <p>For this concrete use case, the mocked {@link DateService} will not
64+
* have {@link Cacheable @Cacheable} applied to it.
65+
*/
66+
@RepeatedTest(2)
67+
void mockShouldNotBeAnAopProxy() {
68+
assertThat(AopUtils.isAopProxy(dateService)).as("is Spring AOP proxy").isFalse();
69+
assertThat(Mockito.mockingDetails(dateService).isMock()).as("is Mockito mock").isTrue();
70+
71+
given(dateService.getDate(false)).willReturn(1L);
72+
Long date = dateService.getDate(false);
73+
assertThat(date).isOne();
74+
75+
given(dateService.getDate(false)).willReturn(2L);
76+
date = dateService.getDate(false);
77+
assertThat(date).isEqualTo(2L);
78+
79+
verify(dateService, times(2)).getDate(false);
80+
verify(dateService, times(2)).getDate(eq(false));
81+
verify(dateService, times(2)).getDate(anyBoolean());
82+
}
83+
84+
85+
@Configuration(proxyBeanMethods = false)
86+
@EnableCaching(proxyTargetClass = true)
87+
@Import(DateService.class)
88+
static class Config {
89+
90+
@Bean
91+
CacheResolver cacheResolver(CacheManager cacheManager) {
92+
return new SimpleCacheResolver(cacheManager);
93+
}
94+
95+
@Bean
96+
ConcurrentMapCacheManager cacheManager() {
97+
return new ConcurrentMapCacheManager("test");
98+
}
99+
}
100+
101+
@Service
102+
static class DateService {
103+
104+
@Cacheable("test")
105+
Long getDate(boolean argument) {
106+
return System.nanoTime();
107+
}
108+
}
109+
110+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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.junit.jupiter.api.Disabled;
20+
import org.junit.jupiter.api.Test;
21+
import org.junit.jupiter.api.extension.ExtendWith;
22+
import org.mockito.Mockito;
23+
24+
import org.springframework.aop.support.AopUtils;
25+
import org.springframework.cache.CacheManager;
26+
import org.springframework.cache.annotation.Cacheable;
27+
import org.springframework.cache.annotation.EnableCaching;
28+
import org.springframework.cache.concurrent.ConcurrentMapCacheManager;
29+
import org.springframework.cache.interceptor.CacheResolver;
30+
import org.springframework.cache.interceptor.SimpleCacheResolver;
31+
import org.springframework.context.annotation.Bean;
32+
import org.springframework.context.annotation.Configuration;
33+
import org.springframework.context.annotation.Import;
34+
import org.springframework.stereotype.Service;
35+
import org.springframework.test.context.junit.jupiter.SpringExtension;
36+
import org.springframework.test.util.AopTestUtils;
37+
38+
import static org.assertj.core.api.Assertions.assertThat;
39+
import static org.mockito.ArgumentMatchers.anyBoolean;
40+
import static org.mockito.ArgumentMatchers.eq;
41+
import static org.mockito.BDDMockito.given;
42+
import static org.mockito.Mockito.doReturn;
43+
import static org.mockito.Mockito.times;
44+
import static org.mockito.Mockito.verify;
45+
46+
/**
47+
* Tests for {@link MockitoSpyBean @MockitoSpyBean} used in combination with Spring AOP.
48+
*
49+
* @author Sam Brannen
50+
* @author Phillip Webb
51+
* @since 6.2
52+
* @see <a href="https://github.com/spring-projects/spring-boot/issues/5837">5837</a>
53+
* @see MockitoBeanAndAopProxyTests
54+
*/
55+
@ExtendWith(SpringExtension.class)
56+
class MockitoSpyBeanAndAopProxyTests {
57+
58+
@MockitoSpyBean
59+
DateService dateService;
60+
61+
62+
/**
63+
* Stubbing and verification for a Mockito spy that is wrapped in a Spring AOP
64+
* proxy should always work when performed via the ultimate target of the Spring
65+
* AOP proxy (i.e., the actual spy instance).
66+
*/
67+
@Test
68+
void stubAndVerifyOnUltimateTargetOfSpringAopProxy() {
69+
assertThat(AopUtils.isAopProxy(dateService)).as("is Spring AOP proxy").isTrue();
70+
DateService spy = AopTestUtils.getUltimateTargetObject(dateService);
71+
assertThat(Mockito.mockingDetails(spy).isSpy()).as("ultimate target is Mockito spy").isTrue();
72+
73+
given(spy.getDate(false)).willReturn(1L);
74+
Long date = dateService.getDate(false);
75+
assertThat(date).isOne();
76+
77+
given(spy.getDate(false)).willReturn(2L);
78+
date = dateService.getDate(false);
79+
assertThat(date).isEqualTo(1L); // 1L instead of 2L, because the AOP proxy caches the original value.
80+
81+
// Each of the following verifies times(1), because the AOP proxy caches the
82+
// original value and does not delegate to the spy on subsequent invocations.
83+
verify(spy, times(1)).getDate(false);
84+
verify(spy, times(1)).getDate(eq(false));
85+
verify(spy, times(1)).getDate(anyBoolean());
86+
}
87+
88+
/**
89+
* Verification for a Mockito spy that is wrapped in a Spring AOP proxy should
90+
* always work when performed via the Spring AOP proxy. However, stubbing
91+
* does not currently work via the Spring AOP proxy.
92+
*
93+
* <p>Consequently, this test method supplies the ultimate target of the Spring
94+
* AOP proxy to stubbing calls, while supplying the Spring AOP proxy to verification
95+
* calls.
96+
*/
97+
@Disabled("Disabled until transparent verification for @MockitoSpyBean is implemented")
98+
@Test
99+
void stubOnUltimateTargetAndVerifyOnSpringAopProxy() {
100+
assertThat(AopUtils.isAopProxy(dateService)).as("is Spring AOP proxy").isTrue();
101+
DateService spy = AopTestUtils.getUltimateTargetObject(dateService);
102+
assertThat(Mockito.mockingDetails(spy).isSpy()).as("ultimate target is Mockito spy").isTrue();
103+
104+
given(spy.getDate(false)).willReturn(1L);
105+
Long date = dateService.getDate(false);
106+
assertThat(date).isOne();
107+
108+
given(spy.getDate(false)).willReturn(2L);
109+
date = dateService.getDate(false);
110+
assertThat(date).isEqualTo(1L); // 1L instead of 2L, because the AOP proxy caches the original value.
111+
112+
// Each of the following verifies times(1), because the AOP proxy caches the
113+
// original value and does not delegate to the spy on subsequent invocations.
114+
verify(dateService, times(1)).getDate(false);
115+
verify(dateService, times(1)).getDate(eq(false));
116+
verify(dateService, times(1)).getDate(anyBoolean());
117+
}
118+
119+
/**
120+
* Ideally, both stubbing and verification should work transparently when a Mockito
121+
* spy is wrapped in a Spring AOP proxy. However, Mockito currently does not provide
122+
* support for transparent stubbing of a proxied spy. For example, implementing a
123+
* custom {@link org.mockito.plugins.MockResolver} will not result in successful
124+
* stubbing for a proxied mock.
125+
*/
126+
@Disabled("Disabled until Mockito provides support for transparent stubbing of a proxied spy")
127+
@Test
128+
void stubAndVerifyDirectlyOnSpringAopProxy() throws Exception {
129+
assertThat(AopUtils.isCglibProxy(dateService)).as("is Spring AOP CGLIB proxy").isTrue();
130+
assertThat(Mockito.mockingDetails(dateService).isSpy()).as("is Mockito spy").isTrue();
131+
132+
doReturn(1L).when(dateService).getDate(false);
133+
Long date = dateService.getDate(false);
134+
assertThat(date).isOne();
135+
136+
doReturn(2L).when(dateService).getDate(false);
137+
date = dateService.getDate(false);
138+
assertThat(date).isEqualTo(1L); // 1L instead of 2L, because the AOP proxy caches the original value.
139+
140+
// Each of the following verifies times(1), because the AOP proxy caches the
141+
// original value and does not delegate to the spy on subsequent invocations.
142+
verify(dateService, times(1)).getDate(false);
143+
verify(dateService, times(1)).getDate(eq(false));
144+
verify(dateService, times(1)).getDate(anyBoolean());
145+
}
146+
147+
148+
@Configuration(proxyBeanMethods = false)
149+
@EnableCaching(proxyTargetClass = true)
150+
@Import(DateService.class)
151+
static class Config {
152+
153+
@Bean
154+
CacheResolver cacheResolver(CacheManager cacheManager) {
155+
return new SimpleCacheResolver(cacheManager);
156+
}
157+
158+
@Bean
159+
ConcurrentMapCacheManager cacheManager() {
160+
return new ConcurrentMapCacheManager("test");
161+
}
162+
}
163+
164+
@Service
165+
static class DateService {
166+
167+
@Cacheable("test")
168+
Long getDate(boolean argument) {
169+
return System.nanoTime();
170+
}
171+
}
172+
173+
}

0 commit comments

Comments
 (0)