简体   繁体   English

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

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

I'm using Groovy with JUnit to test my Java code. 我正在使用GroovyJUnit来测试我的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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM