Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

@MockitoSpyBean does not support transparent verification #33774

Closed
sbrannen opened this issue Oct 23, 2024 · 1 comment
Closed

@MockitoSpyBean does not support transparent verification #33774

sbrannen opened this issue Oct 23, 2024 · 1 comment
Assignees
Labels
in: test Issues in the test module type: bug A general bug
Milestone

Comments

@sbrannen
Copy link
Member

sbrannen commented Oct 23, 2024

Overview

SpringAopBypassingVerificationStartedListener provides partial support for transparent verification for Mockito spies created via @MockitoSpyBean when the spy is wrapped in a Spring AOP proxy.

However, the following @Disabled test currently fails since the AOP proxy for the DateService is not considered a mock by Mockito.

/**
* Verification for a Mockito spy that is wrapped in a Spring AOP proxy should
* always work when performed via the Spring AOP proxy. However, stubbing
* does not currently work via the Spring AOP proxy.
*
* <p>Consequently, this test method supplies the ultimate target of the Spring
* AOP proxy to stubbing calls, while supplying the Spring AOP proxy to verification
* calls.
*/
@Disabled("Disabled until transparent verification for @MockitoSpyBean is implemented")
@Test
void stubOnUltimateTargetAndVerifyOnSpringAopProxy() {
assertThat(AopUtils.isAopProxy(dateService)).as("is Spring AOP proxy").isTrue();
DateService spy = AopTestUtils.getUltimateTargetObject(dateService);
assertThat(Mockito.mockingDetails(spy).isSpy()).as("ultimate target is Mockito spy").isTrue();
given(spy.getDate(false)).willReturn(1L);
Long date = dateService.getDate(false);
assertThat(date).isOne();
given(spy.getDate(false)).willReturn(2L);
date = dateService.getDate(false);
assertThat(date).isEqualTo(1L); // 1L instead of 2L, because the AOP proxy caches the original value.
// Each of the following verifies times(1), because the AOP proxy caches the
// original value and does not delegate to the spy on subsequent invocations.
verify(dateService, times(1)).getDate(false);
verify(dateService, times(1)).getDate(eq(false));
verify(dateService, times(1)).getDate(anyBoolean());
}

To provide transparent verification support for @MockitoSpyBean, we need to implement and register a Mockito MockResolver, analogous to the SpringBootMockResolver in Spring Boot.

Related Issues

@sbrannen sbrannen added in: test Issues in the test module type: bug A general bug labels Oct 23, 2024
@sbrannen sbrannen added this to the 6.2.0-RC3 milestone Oct 23, 2024
@sbrannen sbrannen self-assigned this Oct 23, 2024
@sbrannen sbrannen changed the title Support transparent verfication for @MockitoSpyBean @MockitoSpyBean does not support transparent verification Oct 23, 2024
@philwebb
Copy link
Member

This unfortunately breaks code that doesn't have spring-aop on the classpath.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
in: test Issues in the test module type: bug A general bug
Projects
None yet
Development

No branches or pull requests

3 participants