Introduce functional interfaces to improve Java 8 utilisation of Mockito 2
See original GitHub issueThere are two places where this would be very useful:
- Custom matchers - used with argThat
- Custom answers - for situations where you would normally create a subclass of Answer<T>
Some ideas here - https://solidsoft.wordpress.com/2015/06/30/more-compact-mockito-with-java-8-lambda-expressions-and-mockito-java8-add-ons/ - have proposed Java 8 specific extensions outside of mockito, but a small modification to mockito itself, usable by Java 8 but not dependent on it, would be sufficient to get a lot of benefit.
Example - if I wanted to check that the object passed to something had a child with the right value I should be able to write
verify(mock, times(1)).someMethod(
argThat(input -> input.getChild().getName().equals("Jim")));
Or if I wanted to answer a request
doAnswer(answer(
string -> string.length()
)).when(mock).execute(anyString());
I will be submitting a pull request to demonstrate these capabilities
Issue Analytics
- State:
- Created 8 years ago
- Reactions:2
- Comments:7 (7 by maintainers)
Top Results From Across the Web
Mockito's Java 8 Features - Baeldung
Overview of Java 8 support in Mockito framework, including Streams and default interface methods.
Read more >Java 8 Functional Interfaces | DigitalOcean
Welcome to the Java 8 functional interfaces example tutorial. ... and new Stream API is introduced that uses a lot of functional interfaces....
Read more >Interface Enhancements In Java 8 - Java Functional Interface
This tutorial explains additions to interfaces in Java 8 and difference between concepts like abstract classes, extends keyword vs ...
Read more >Mocking and verifying Java functional interfaces with Mockito
Our goal is to unit test such application and verify communication to the out-of-process dependency including data passed to the functional ...
Read more >Consumer Functional Interface in Java - Apps Developer Blog
The Consumer interface is an in-built Functional interface introduced in Java 8, and it is part of the java.util.function package.
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
@ashleyfrieze What do you think about adding
doAnswer(AnswerXXX answer)
overloads to the classMockito
?This would eliminate the need of the answer(…) wapper function.
doAnswer(answer((x,y)->x*y))
vs .doAnswer((x,y)->x*y)
Thanks for the comment. I was feeling a bit ignored 😃
The answer is that these are harder to use than the change I’m proposing.
In the case of the boiler plate
My proposal provides a wrapper that can infer all the types and avoid you having to manipulate the invocation object directly.
In the case of the argument matcher, I took the code which I think was probably made that way to comply with something from Hamcrest, which uses Object as the input parameter to the ArgumentMatcher<T> rather than T and just made it more strongly typed. This again reduces the need for boilerplate.