How to generate a Java method reference using Groovy for testing purposes

8.2k views Asked by At

I'm using Groovy with JUnit to test my Java code.

I need to test a method foo() which takes in a java.util.function.Function

public void foo(Function<Foo,Bar> func){
    return null; 
}

In my normal code I call foo by passing in a method reference of a method bar ie.

foo(mybar::bar)

How can I test this function in Groovy elegantly?

Using:

mybar.&bar

yields a groovy.lang.Closure<...> which is not compatible with java.util.function.Function.

How else can I achieve this?

1

There are 1 answers

0
Roger Glover On BEST ANSWER

Coerce the final attempt to Function, like this:

foo(mybar.&bar as Function)