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

ᴘᴀɴᴀʏɪᴏᴛɪs :

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?

BalRog :

Coerce the final attempt to Function, like this:

foo(mybar.&bar as Function)

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=454425&siteId=1