Skip to content

Commit 3684c2e

Browse files
committed
Ensure argument matchers work with AOP spies
Update MockitoAopProxyTargetInterceptor to deal with deal with any existing argument matchers when working with the VerificationMode. Prior to this commit `@SpyBean` when combined with AOP could not support argument matchers. Fixes gh-6871
1 parent 41a36c4 commit 3684c2e

File tree

4 files changed

+39
-15
lines changed

4 files changed

+39
-15
lines changed

spring-boot-test/src/main/java/org/springframework/boot/test/mock/mockito/MockitoAopProxyTargetInterceptor.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,15 @@
1717
package org.springframework.boot.test.mock.mockito;
1818

1919
import java.lang.reflect.Field;
20+
import java.util.List;
2021

2122
import org.aopalliance.aop.Advice;
2223
import org.aopalliance.intercept.Interceptor;
2324
import org.aopalliance.intercept.MethodInterceptor;
2425
import org.aopalliance.intercept.MethodInvocation;
2526
import org.mockito.internal.InternalMockHandler;
27+
import org.mockito.internal.matchers.LocalizedMatcher;
28+
import org.mockito.internal.progress.ArgumentMatcherStorage;
2629
import org.mockito.internal.progress.MockingProgress;
2730
import org.mockito.internal.stubbing.InvocationContainer;
2831
import org.mockito.internal.util.MockUtil;
@@ -107,7 +110,7 @@ public boolean isVerifying() {
107110
synchronized (this.monitor) {
108111
VerificationMode mode = this.progress.pullVerificationMode();
109112
if (mode != null) {
110-
this.progress.verificationStarted(mode);
113+
resetVerificationStarted(mode);
111114
return true;
112115
}
113116
return false;
@@ -124,11 +127,20 @@ public void replaceVerifyMock(Object source, Object target) {
124127
mode = new MockAwareVerificationMode(target, mockAwareMode);
125128
}
126129
}
127-
this.progress.verificationStarted(mode);
130+
resetVerificationStarted(mode);
128131
}
129132
}
130133
}
131134

135+
private void resetVerificationStarted(VerificationMode mode) {
136+
ArgumentMatcherStorage storage = this.progress.getArgumentMatcherStorage();
137+
List<LocalizedMatcher> matchers = storage.pullLocalizedMatchers();
138+
this.progress.verificationStarted(mode);
139+
for (LocalizedMatcher matcher : matchers) {
140+
storage.reportMatcher(matcher);
141+
}
142+
}
143+
132144
}
133145

134146
}

spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/MockBeanWithAopProxyTests.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535

3636
import static org.assertj.core.api.Assertions.assertThat;
3737
import static org.mockito.BDDMockito.given;
38+
import static org.mockito.Matchers.anyBoolean;
39+
import static org.mockito.Matchers.eq;
3840
import static org.mockito.Mockito.times;
3941
import static org.mockito.Mockito.verify;
4042

@@ -52,13 +54,15 @@ public class MockBeanWithAopProxyTests {
5254

5355
@Test
5456
public void verifyShouldUseProxyTarget() throws Exception {
55-
given(this.dateService.getDate()).willReturn(1L);
56-
Long d1 = this.dateService.getDate();
57+
given(this.dateService.getDate(false)).willReturn(1L);
58+
Long d1 = this.dateService.getDate(false);
5759
assertThat(d1).isEqualTo(1L);
58-
given(this.dateService.getDate()).willReturn(2L);
59-
Long d2 = this.dateService.getDate();
60+
given(this.dateService.getDate(false)).willReturn(2L);
61+
Long d2 = this.dateService.getDate(false);
6062
assertThat(d2).isEqualTo(2L);
61-
verify(this.dateService, times(2)).getDate();
63+
verify(this.dateService, times(2)).getDate(false);
64+
verify(this.dateService, times(2)).getDate(eq(false));
65+
verify(this.dateService, times(2)).getDate(anyBoolean());
6266
}
6367

6468
@Configuration
@@ -86,7 +90,7 @@ public ConcurrentMapCacheManager cacheManager() {
8690
static class DateService {
8791

8892
@Cacheable(cacheNames = "test")
89-
public Long getDate() {
93+
public Long getDate(boolean argument) {
9094
return System.nanoTime();
9195
}
9296

spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanWithAopProxyAndNotProxyTargetAwareTests.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.springframework.stereotype.Service;
3535
import org.springframework.test.context.junit4.SpringRunner;
3636

37+
import static org.mockito.Matchers.anyBoolean;
38+
import static org.mockito.Matchers.eq;
3739
import static org.mockito.Mockito.reset;
3840
import static org.mockito.Mockito.times;
3941
import static org.mockito.Mockito.verify;
@@ -52,8 +54,10 @@ public class SpyBeanWithAopProxyAndNotProxyTargetAwareTests {
5254

5355
@Test(expected = UnfinishedVerificationException.class)
5456
public void verifyShouldUseProxyTarget() throws Exception {
55-
this.dateService.getDate();
56-
verify(this.dateService, times(1)).getDate();
57+
this.dateService.getDate(false);
58+
verify(this.dateService, times(1)).getDate(false);
59+
verify(this.dateService, times(1)).getDate(eq(false));
60+
verify(this.dateService, times(1)).getDate(anyBoolean());
5761
reset(this.dateService);
5862
}
5963

@@ -82,7 +86,7 @@ public ConcurrentMapCacheManager cacheManager() {
8286
static class DateService {
8387

8488
@Cacheable(cacheNames = "test")
85-
public Long getDate() {
89+
public Long getDate(boolean arg) {
8690
return System.nanoTime();
8791
}
8892

spring-boot-test/src/test/java/org/springframework/boot/test/mock/mockito/SpyBeanWithAopProxyTests.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.springframework.test.context.junit4.SpringRunner;
3535

3636
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.mockito.Matchers.anyBoolean;
38+
import static org.mockito.Matchers.eq;
3739
import static org.mockito.Mockito.times;
3840
import static org.mockito.Mockito.verify;
3941

@@ -51,11 +53,13 @@ public class SpyBeanWithAopProxyTests {
5153

5254
@Test
5355
public void verifyShouldUseProxyTarget() throws Exception {
54-
Long d1 = this.dateService.getDate();
56+
Long d1 = this.dateService.getDate(false);
5557
Thread.sleep(200);
56-
Long d2 = this.dateService.getDate();
58+
Long d2 = this.dateService.getDate(false);
5759
assertThat(d1).isEqualTo(d2);
58-
verify(this.dateService, times(1)).getDate();
60+
verify(this.dateService, times(1)).getDate(false);
61+
verify(this.dateService, times(1)).getDate(eq(false));
62+
verify(this.dateService, times(1)).getDate(anyBoolean());
5963
}
6064

6165
@Configuration
@@ -83,7 +87,7 @@ public ConcurrentMapCacheManager cacheManager() {
8387
static class DateService {
8488

8589
@Cacheable(cacheNames = "test")
86-
public Long getDate() {
90+
public Long getDate(boolean arg) {
8791
return System.nanoTime();
8892
}
8993

0 commit comments

Comments
 (0)