def 'test of java lambda' () {
given:
Callable<String> working = () -> {return "working"}
and:
def executor = Executors.newFixedThreadPool(2)
when:
def result = executor.submit(working)
Thread.sleep(1000)
then:
result.get() != null
}
这个测试不会通过。 返回值始终为空。
如何使用 java lambda 来让这个测试通过?
Groovy 正在选择提交方法的 Runnable 变体
它返回一个返回 null 的 future
要强制它使用 Callable 变体,您可以强制转换参数
def result = executor.submit((Callable) working)