如何使用Groovy生成Java方法引用以进行测试

时间:2022-12-30 19:03:29

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

我正在使用Groovy和JUnit来测试我的Java代码。

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

我需要测试一个接受java.util.function.Function的方法foo()

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,即。

foo(mybar::bar)

How can I test this function in Groovy elegantly?

如何优雅地在Groovy中测试这个功能?

Using:

mybar.&bar

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

产生一个与java.util.function.Function不兼容的groovy.lang.Closure <...>。

How else can I achieve this?

我怎么能做到这一点?

1 个解决方案

#1


8  

Coerce the final attempt to Function, like this:

强制执行Function的最终尝试,如下所示:

foo(mybar.&bar as Function)

#1


8  

Coerce the final attempt to Function, like this:

强制执行Function的最终尝试,如下所示:

foo(mybar.&bar as Function)