Commit 3684c2ec authored by Phillip Webb's avatar Phillip Webb

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
parent 41a36c4d
...@@ -17,12 +17,15 @@ ...@@ -17,12 +17,15 @@
package org.springframework.boot.test.mock.mockito; package org.springframework.boot.test.mock.mockito;
import java.lang.reflect.Field; import java.lang.reflect.Field;
import java.util.List;
import org.aopalliance.aop.Advice; import org.aopalliance.aop.Advice;
import org.aopalliance.intercept.Interceptor; import org.aopalliance.intercept.Interceptor;
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; import org.aopalliance.intercept.MethodInvocation;
import org.mockito.internal.InternalMockHandler; import org.mockito.internal.InternalMockHandler;
import org.mockito.internal.matchers.LocalizedMatcher;
import org.mockito.internal.progress.ArgumentMatcherStorage;
import org.mockito.internal.progress.MockingProgress; import org.mockito.internal.progress.MockingProgress;
import org.mockito.internal.stubbing.InvocationContainer; import org.mockito.internal.stubbing.InvocationContainer;
import org.mockito.internal.util.MockUtil; import org.mockito.internal.util.MockUtil;
...@@ -107,7 +110,7 @@ class MockitoAopProxyTargetInterceptor implements MethodInterceptor { ...@@ -107,7 +110,7 @@ class MockitoAopProxyTargetInterceptor implements MethodInterceptor {
synchronized (this.monitor) { synchronized (this.monitor) {
VerificationMode mode = this.progress.pullVerificationMode(); VerificationMode mode = this.progress.pullVerificationMode();
if (mode != null) { if (mode != null) {
this.progress.verificationStarted(mode); resetVerificationStarted(mode);
return true; return true;
} }
return false; return false;
...@@ -124,11 +127,20 @@ class MockitoAopProxyTargetInterceptor implements MethodInterceptor { ...@@ -124,11 +127,20 @@ class MockitoAopProxyTargetInterceptor implements MethodInterceptor {
mode = new MockAwareVerificationMode(target, mockAwareMode); mode = new MockAwareVerificationMode(target, mockAwareMode);
} }
} }
this.progress.verificationStarted(mode); resetVerificationStarted(mode);
} }
} }
} }
private void resetVerificationStarted(VerificationMode mode) {
ArgumentMatcherStorage storage = this.progress.getArgumentMatcherStorage();
List<LocalizedMatcher> matchers = storage.pullLocalizedMatchers();
this.progress.verificationStarted(mode);
for (LocalizedMatcher matcher : matchers) {
storage.reportMatcher(matcher);
}
}
} }
} }
...@@ -35,6 +35,8 @@ import org.springframework.test.context.junit4.SpringRunner; ...@@ -35,6 +35,8 @@ import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
...@@ -52,13 +54,15 @@ public class MockBeanWithAopProxyTests { ...@@ -52,13 +54,15 @@ public class MockBeanWithAopProxyTests {
@Test @Test
public void verifyShouldUseProxyTarget() throws Exception { public void verifyShouldUseProxyTarget() throws Exception {
given(this.dateService.getDate()).willReturn(1L); given(this.dateService.getDate(false)).willReturn(1L);
Long d1 = this.dateService.getDate(); Long d1 = this.dateService.getDate(false);
assertThat(d1).isEqualTo(1L); assertThat(d1).isEqualTo(1L);
given(this.dateService.getDate()).willReturn(2L); given(this.dateService.getDate(false)).willReturn(2L);
Long d2 = this.dateService.getDate(); Long d2 = this.dateService.getDate(false);
assertThat(d2).isEqualTo(2L); assertThat(d2).isEqualTo(2L);
verify(this.dateService, times(2)).getDate(); verify(this.dateService, times(2)).getDate(false);
verify(this.dateService, times(2)).getDate(eq(false));
verify(this.dateService, times(2)).getDate(anyBoolean());
} }
@Configuration @Configuration
...@@ -86,7 +90,7 @@ public class MockBeanWithAopProxyTests { ...@@ -86,7 +90,7 @@ public class MockBeanWithAopProxyTests {
static class DateService { static class DateService {
@Cacheable(cacheNames = "test") @Cacheable(cacheNames = "test")
public Long getDate() { public Long getDate(boolean argument) {
return System.nanoTime(); return System.nanoTime();
} }
......
...@@ -34,6 +34,8 @@ import org.springframework.context.annotation.Import; ...@@ -34,6 +34,8 @@ import org.springframework.context.annotation.Import;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.reset; import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
...@@ -52,8 +54,10 @@ public class SpyBeanWithAopProxyAndNotProxyTargetAwareTests { ...@@ -52,8 +54,10 @@ public class SpyBeanWithAopProxyAndNotProxyTargetAwareTests {
@Test(expected = UnfinishedVerificationException.class) @Test(expected = UnfinishedVerificationException.class)
public void verifyShouldUseProxyTarget() throws Exception { public void verifyShouldUseProxyTarget() throws Exception {
this.dateService.getDate(); this.dateService.getDate(false);
verify(this.dateService, times(1)).getDate(); verify(this.dateService, times(1)).getDate(false);
verify(this.dateService, times(1)).getDate(eq(false));
verify(this.dateService, times(1)).getDate(anyBoolean());
reset(this.dateService); reset(this.dateService);
} }
...@@ -82,7 +86,7 @@ public class SpyBeanWithAopProxyAndNotProxyTargetAwareTests { ...@@ -82,7 +86,7 @@ public class SpyBeanWithAopProxyAndNotProxyTargetAwareTests {
static class DateService { static class DateService {
@Cacheable(cacheNames = "test") @Cacheable(cacheNames = "test")
public Long getDate() { public Long getDate(boolean arg) {
return System.nanoTime(); return System.nanoTime();
} }
......
...@@ -34,6 +34,8 @@ import org.springframework.stereotype.Service; ...@@ -34,6 +34,8 @@ import org.springframework.stereotype.Service;
import org.springframework.test.context.junit4.SpringRunner; import org.springframework.test.context.junit4.SpringRunner;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.times; import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
...@@ -51,11 +53,13 @@ public class SpyBeanWithAopProxyTests { ...@@ -51,11 +53,13 @@ public class SpyBeanWithAopProxyTests {
@Test @Test
public void verifyShouldUseProxyTarget() throws Exception { public void verifyShouldUseProxyTarget() throws Exception {
Long d1 = this.dateService.getDate(); Long d1 = this.dateService.getDate(false);
Thread.sleep(200); Thread.sleep(200);
Long d2 = this.dateService.getDate(); Long d2 = this.dateService.getDate(false);
assertThat(d1).isEqualTo(d2); assertThat(d1).isEqualTo(d2);
verify(this.dateService, times(1)).getDate(); verify(this.dateService, times(1)).getDate(false);
verify(this.dateService, times(1)).getDate(eq(false));
verify(this.dateService, times(1)).getDate(anyBoolean());
} }
@Configuration @Configuration
...@@ -83,7 +87,7 @@ public class SpyBeanWithAopProxyTests { ...@@ -83,7 +87,7 @@ public class SpyBeanWithAopProxyTests {
static class DateService { static class DateService {
@Cacheable(cacheNames = "test") @Cacheable(cacheNames = "test")
public Long getDate() { public Long getDate(boolean arg) {
return System.nanoTime(); return System.nanoTime();
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment