question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Introduce functional interfaces to improve Java 8 utilisation of Mockito 2

See original GitHub issue

There are two places where this would be very useful:

  1. Custom matchers - used with argThat
  2. 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:closed
  • Created 8 years ago
  • Reactions:2
  • Comments:7 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
ChristianSchwarzcommented, Jun 8, 2016

@ashleyfrieze What do you think about adding doAnswer(AnswerXXX answer) overloads to the class Mockito?

This would eliminate the need of the answer(…) wapper function.

doAnswer(answer((x,y)->x*y)) vs . doAnswer((x,y)->x*y)

1reaction
ashleyfriezecommented, Mar 3, 2016

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

invocation.getArgumentAt(0, String.class).length()

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found