-
-
Notifications
You must be signed in to change notification settings - Fork 314
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
How to mock method reference? #213
Comments
Method references are not always the same. So it is a good question. I will have to dig into it. If the same method reference is passed it works.
|
In my case I have 3 specific method references and then one general purpose one, I need to be sure each are set correctly. |
Can't you test that calling it gives the right behavior? |
No, I have no idea how to specify the method reference. I have tried a bunch of things like this: `
` Where instance is the JTaskerHandler class instance under test. But that fails with this: So it doesn't like that. I would be okay if it was just matching the 'name' of the method but I have no idea how to do that either. |
I've put a bunch of experts on the topic. So far the answer is: "Not possible". The method reference is transformed into a lambda which is a class of its own. And the name of the referenced method isn't kept apart in the bytecode of the core of the lambda. It seems to be a Java quirk. |
Yeah somehow EasyMock will likely have to be changed to support new Java
features like this. I don't like it but one option might be to add
EasyMock annotations on method references. Another less desirable solution
might be to 'capture' the method instead of 'expecting' it, then the
captured argument would have to have a way to call/trigger it so it can be
tested. That's not as desirable as it means I have to do both 'expect' and
'capture' just to test one method but I have separate tests for the method
reference behavior anyway so might not be too bad of a solution.
…-Dave
On Fri, Apr 13, 2018 at 8:17 AM, Henri Tremblay ***@***.***> wrote:
I've put a bunch of experts on the topic. So far the answer is: "Not
possible". The method reference is transformed into a lambda which is a
class of its own. And the name of the referenced method isn't kept apart in
the bytecode of the core of the lambda. It seems to be a Java quirk.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#213 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/ABfwr8-Tk1sZ1Da2y10S1WgstKU7V1orks5toLN3gaJpZM4TSbjT>
.
|
It's not EasyMock. It's Java that doesn't allow it. No equals on method reference possible. You can do two things:
Both already works.
|
I had a scenario where I was passing a method reference to another method My solution was as follows: Set an expectation on the method you expect to pass
Set the expectation on the method to which it is passed and capture the lambda
Invoke the tested method , which satisfies the second expectation.
invoke the captured lambda to satisfy the first expectation and check the right method reference got passed.
|
How would I mock a JDK8 method reference? I want to know that the right method name was passed.
E.g. here is the method to test:
Here is my current test but it's missing any real purpose because I can't figure out how to specify the correct method reference.
The text was updated successfully, but these errors were encountered: