Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Data Driven Spock test fails with upgrade to groovy 2.5

Tags:

java

groovy

spock

When the variable in the where clause is named with the same name in the other test but the types are different, the data driven test is trying to cast it to the wrong type.

org.codehaus.groovy:groovy-all:2.5.6
org.spockframework:spock-core:1.3-groovy-2.5
cglib:cglib-nodep:3.2.12

The error that I am getting is

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'str' with class 'java.lang.String' to class 'com.togise.hello.Hello'

If I rename the variable in the where clause to something else the test passes.

The code can be found at AppTest.groovy

Here is code as well

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
    }
}

like image 992
togise Avatar asked Nov 24 '25 07:11

togise


1 Answers

It seems this is a known bug https://github.com/spockframework/spock/issues/880 and it is still open.

The workaround is to rename the variable in the where clause for now.

like image 76
togise Avatar answered Nov 25 '25 20:11

togise



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!