【问题标题】:Data Driven Spock test fails with upgrade to groovy 2.5升级到 groovy 2.5 时数据驱动的 Spock 测试失败
【发布时间】: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
    }
}

【问题讨论】:

    标签: java groovy spock


    【解决方案1】:

    这似乎是一个已知的错误https://github.com/spockframework/spock/issues/880,它仍然处于打开状态。

    解决方法是暂时重命名 where 子句中的变量。

    【讨论】:

    • 该错误是由 groovy-2.5 而不是升级到 java 11 引起的,您可能需要编辑文本。如果您使用 groovy-2.5,您将在 java 8 上遇到相同的错误。
    最近更新 更多