[英]How to generate a Java method reference using Groovy for testing purposes
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. 在我的正常代码中,我通过传入方法bar
的方法引用来调用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? 我怎么能做到这一点?
强制执行Function
的最终尝试,如下所示:
foo(mybar.&bar as Function)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.