【发布时间】:2019-10-08 02:51:25
【问题描述】:
我想测试将null 传递给这些重载方法:
public static Object someMethod(String n) { /* some impl */ }
public static Object someMethod(Integer n) { /* some impl */ }
我试过了:
def "test someMethod"() {
expect:
someMethod(input) == expected
where:
input | expected
null as String | someValue
null as Integer | someValue
}
但我得到了错误:
groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.foo.MyClass#someMethod.
Cannot resolve which method to invoke for [null] due to overlapping prototypes between:
[class java.lang.String]
[class java.lang.Integer]
如何使用一种 spock 方法在 where 块中输入空值(使用其他值)来测试这些?
【问题讨论】:
-
不知道有没有用。在 where 部分尝试定义闭包:
{ someMethod(null as String) } | someValue和期望部分input()==expected -
我试图用 Spock 1.3 和 Groovy 2.5.8 重现您的问题,但不能。相反,我遇到了另一个 Spock 问题,请参阅 here。您必须使用其他版本的 Spock 和/或 Groovy。