【发布时间】:2019-05-09 15:36:09
【问题描述】:
当 where 子句中的变量在另一个测试中以相同的名称命名但类型不同时,数据驱动测试会尝试将其转换为错误的类型。
org.codehaus.groovy:groovy-all:2.5.6
org.spockframework:spock-core:1.3-groovy-2.5
cglib:cglib-nodep:3.2.12
我得到的错误是
org.codehaus.groovy.runtime.typehandling.GroovyCastException: 不能 将具有类“java.lang.String”的对象“str”转换为类 'com.togise.hello.Hello'
如果我将 where 子句中的变量重命名为其他名称,则测试通过。
代码可以在AppTest.groovy找到
这里也是代码
package com.togise
import com.togise.hello.Hello
import spock.lang.Specification
class AppTest extends Specification {
def "application has a greeting"() {
setup:
def app = new App()
Hello hello = new Hello(str: "str")
when:
def result = app.greeting
then:
result != null
hello != null
}
def "when the variable in the where clause is named with the same name with in some other test in this class but the types are different, the test is trying to cast it to the wrong type"() {
App app = Mock()
when:
new String()
then:
app.getGreeting() >> hello
where:
hello << ["str"] // matches the name of the variable in the previous test
}
}
【问题讨论】: