|
| 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