-
Notifications
You must be signed in to change notification settings - Fork 40.9k
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
Recommend the use of AopTestUtils.getTargetObject() when setting expectations on a spied bean that Spring has proxied #22281
Comments
To fix this, we need to be able to automatically unwrap the proxy as is being done here: doNothing()
.when(
// fetch spy bean by unwrapping the AOP proxy, if any
AopTestUtils.<KafkaService>getTargetObject(kafkaService)
)
.send(ArgumentMatchers.any()); Unfortunately, I don't think there's anything in Mockito's current public API that will notify us of the @mockitoguy If you have a moment, I'd love your expertise here please. I'm wondering if there's something that I've missed in Mockito that would provide the necessary interception point. |
Hey! One thing to try is to see if "InvocationListener" works for your use case. I don't remember off top of my head if it reports stubbings or only regular invocations (and whether it works with when and doReturn stubbings) Adding new listener is conceivable. My understanding is that the "sample" link in the description reproduces this very issue? I'm a little busy next couple of days but I'm happy help. |
Thanks, @mockitoguy. Thinking about this some more, I'm not sure that we can fix this at the Mockito level. If the method being stubbed returned something other than I'm starting to believe that we may not always be able to do the right thing when proxying is involved. We may just have to make working with that proxy and getting its underlying target easier and more obvious. |
We've opened spring-projects/spring-framework#33743 to provide a nicer API for dealing with spies that are also Spring proxies. In the meantime, manually removing the Spring proxies by calling |
@SpyBean
Commit b53f54f incorrectly states:
Whereas, that should read:
|
Sounds good, thanks! |
With thanks to @kriegaex, see this question on Stack Overflow and the sample to which it links.
The text was updated successfully, but these errors were encountered: