feature request: spying on lambdas
See original GitHub issueHere’s some Java8 code that works today (even with Mockito 1.x):
Function<Integer, Integer> incrementer = x -> x + 1;
Function<Integer, Integer> spyIncrementer =
mock(Function.class, AdditionalAnswers.delegatesTo(incrementer));
I then use spyIncrementer
as an argument to the code that I really want to test and write a series of verify
calls to check how spyIncrementer
was used. In particular, I’m testing a memoization system that should only ever call call this lambda once with any given argument, so there are a series of Mockito calls along the lines of verify(spyIncrementer, atMost(1)).apply(1);
It would be preferable to be able to write something more like:
Function<Integer, Integer> spyIncrementer = spy(x -> x + 1);
With org.mockito:mockito-inline:2.7.22
, this yields a NoClassDefFoundError
. If you instead use org.mockito:mockito-core:2.7.22
, you get Mockito cannot mock/spy because : - final class.
Issue Analytics
- State:
- Created 6 years ago
- Reactions:5
- Comments:5 (3 by maintainers)
Top Results From Across the Web
Using AWS Lambda with AWS X-Ray - AWS Documentation
You can use AWS X-Ray to visualize the components of your application, identify performance bottlenecks, and troubleshoot requests that resulted in an error ......
Read more >LambdaSpy - Implanting the Lambda execution environment ...
In part one of this series, we discussed the AWS Lambda execution environment. In this post, we focus on the challenge of persisting...
Read more >Serverless Spy Vs Spy Chapter 1: X-ray - DEV Community
In serverless development, we have the problem of the "missing cloud profiler". We cannot look into the distributed Lambda Function/services to ...
Read more >Do you do Lambda Spaghetti? | tecRacer Amazon AWS Blog
The first part of the solution is to run Lambda functions without the Lambda resources. Mini Monolith without lambda. To have a fast...
Read more >Feature Request: AWS Lambda
There are some peculiar packaging and deployment requirements that might be handled very nicely by the some IDE features. Dependency files get ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Yes please. Sorry for not getting to this issue sooner. We should have commented 😢 Your help is still appreciated though, so feel free to sent more PRs! Issues tagged with “Please contribute” are definitely open for community PRs: https://github.com/mockito/mockito/issues?q=is%3Aissue+is%3Aopen+label%3A"please+contribute"
@TimvdLippe sorry for commenting on a closed issue, but can you reconsider reopening it? We might have missed the point of spying on a lambda. I don’t think the idea would be to stub the methods or something like that, but to verify their recorded information. For instance, this becomes extremely useful when we want to test callbacks, and we only care if the lambda was called, how many times, which arguments, etc.
Similar libraries in other languages have this feature since verifying is the most useful part of a
spy()
. A good example is Sinon.JS, in which we even find an example of this usage in the documentation: https://sinonjs.org/releases/v13/spies/I found myself in this same situation while improving some tests on a library of my authoring called Maybe. Here’s the gist of the use case 🙂
As you can see, the test only cares to verify if the
effect
callback was invoked once with the specific argument. I think there’s no other way of doing this with Mockito, and since spying to the lambda will fail, I’m using @danwallach’s workaround for now.I hope you find this helpful in improving Mockito 🙂
Cheers!