-
Notifications
You must be signed in to change notification settings - Fork 40.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow proxied @SpyBeans to be used with Mockito's inline mock maker #22416
Comments
@jacky1193610322 Thanks for the report. We don't officially support Mockito 3.4 yet, but it's likely that we will want to do so in Spring Boot 2.4. To help us to investigate your problem, can you please provide a minimal sample that reproduces it? |
If you would like us to look at this issue, please provide the requested information. If the information is not provided within the next 7 days this issue will be closed. |
sorry,I will offer simple code,but because of the rule of company, I can't upload the whole project, I will paste the core code. |
@jacky1193610322 it doesn't have to be your project. Actually, it has to be a minimal sample and that shouldn't expose any company specific information. Please don't paste the core code as the only thing we could do is try to copy/paste that in an actual project we can run. |
I know. but push the code to GitHub is forbid. |
You can share the code in a different form (attaching a zip to this issue) or you can send it to me by email if that's easier. My email is on my github profile. |
ok |
sorry, the mail can't work now, can't upload code, but the example is simple, any aop proxy can reproduction. @Component
@org.aspectj.lang.annotation.Aspect
public class Aspect {
@Pointcut("execution(* com.example.demo.*.*(..))")
public void pointcut() {
}
@Around("pointcut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
try {
return joinPoint.proceed();
} catch (Throwable e) {
throw e;
} finally {
System.out.println("around");
}
}
} http action @RequestMapping(value = "/demo/", produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
public class Action {
public void test() {
}
} test @SpringBootTest
class ActionTest {
@SpyBean
private Action action;
@Test
void name() {
doNothing().when(action).test();
Mockito.verify(action);
}
} |
Looking more closely, I think this is a Mockito bug or at least a limitation. As a result of using if (!(mock instanceof MockAccess)) {
return null;
}
return ((MockAccess) mock).getMockitoInterceptor().getMockHandler();
In the failing case, MockMethodInterceptor interceptor = mocks.get(mock);
if (interceptor == null) {
return null;
} else {
return interceptor.handler;
}
Given the same input to their The problem can be worked around by removing Spring's proxy before passing the spy into Mockito: Action actionSpy = AopTestUtils.<Action>getTargetObject(action);
doNothing().when(actionSpy).test();
action.test();
verify(actionSpy).test(); |
Cross-posting from our issue tracker: TLNR; this is not a Mockito bug, it's Spring relying on an undocumented implementation detail that might just change in any bugfix release. The reason things currently (!) work with the The reason it does not work with the From Mockito's side things are simple: if one does not provide "our" mock object to "our" API, we do not guarantee to function, not in the past, nor the future. That it does currently work with the subclass mock maker facility is rather accidental and might change in the future. If you implemented the |
It looks like we'll be able to address this with a custom |
Great. new Mockito release to Central is underway just now. |
I just realized we actually didn't publish a new minor version to Maven Central. However, I just tried to publish 3.6.0 and Bintray had issues. Hopefully we can figure out soon how to resolve that. |
We have been able to fix our release woes and 3.6.0 has been published to Maven Central: https://repo1.maven.org/maven2/org/mockito/mockito-core/3.6.0/ |
Thanks @TimvdLippe. The upgrade is scheduled for |
Closed by d9084ea. |
Hi @wilkinsona, I think Thanks, |
spring-boot version is 2.1.3.RELEASE
I already addressed the issue in mockito. issue
the mockito 3.4.0 can mock static method, so I upgrade mockito to 3.4.0, but the mock framework changed, now when I use
the @SpyBean and then verify it. it will throw NotAMockException.
The text was updated successfully, but these errors were encountered: