-
Notifications
You must be signed in to change notification settings - Fork 40.2k
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
Mocking session scoped spy bean calls real method #17817
Comments
@cdalexndr Please provide a minimal sample that we can use to reproduce the issue. |
https://github.com/cdalexndr/spring-boot-issue-17817 |
I did some debugging. Seems like that |
Thanks for the sample, @cdalexndr. When you use I can't see a way to fix this without some changes in Spring Framework. Flagging for team attention in case I've missed something. |
We might be able to get the bean definition for the target as both it and the scoped proxy are added to the bean factory. We could perhaps then replace the target with the definition for the spy. |
@cdalexndr While we're working on a fix, you can work around the problem by spying upon the scoped proxy's target which is the underlying
|
Hi @wilkinsona ,
|
That looks possible. It's certainly in the same area. Please open a new issue with a small sample that reproduces the issue and we can take a look. |
Please note that, unfortunately, we've had to revert the fix for this issue. The problem can be avoided by adding a dependency on |
@wilkinsona if that dependency fixes this issue, shouldn't be added to spring-boot-starter-test to be general available? |
No, I don't think so. It fundamentally changes the way in which Mockito behaves and the Mockito team have chosen to make it opt-in for that reason. If Mockito makes it the default in the future then we would probably do the same in Spring Boot. Until then, I think it should remain opt-in so that users have a natural point at which to consider the implications. |
@wilkinsona then if the fix was reverted and the workaround cannot be made general available due to side effects, shouldn't this issue be reopened, as a reminder, until a new fix is made? |
We've already made the only "fix" that we can, and that is to add a note to the documentation about |
Trying to mock a
@SessionScope
in a test by autowiring it using@SpyBean
and then mocking usingdoReturn(..).when(sessionScopedBean).method(any())
calls the real method with null argument instead of silently registering mock.Removing
@SessionScope
from the bean resolves this issue.Spring boot 2.1.6.RELEASE
The text was updated successfully, but these errors were encountered: