Search code examples
groovycastingspockclasscastexceptionbuilder

Why two separate tests are dependent in Spock?


I have two totally independent tests verifying two different builders (simplified as much as possible). The second one is failing, but there is no reason for that. Why is it happening?

def "first"() {
    StringBuilder builder

    expect: true
}

def "second"() {
    expect: true

    where:
    builder << [new ProcessBuilder()]
}

org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'java.lang.ProcessBuilder@186f8716' with class 'java.lang.ProcessBuilder' to class 'java.lang.StringBuilder'


Solution

  • This question is actually a duplicate of that one.

    Bottom line: You have hit bug #880 in Spock which has been resolved about a week ago and the fix of which you can probably enjoy in the final version Spock 2.0 or in the next milestone release if there is one more before the final one.

    Workaround: rename the second variable to something else.

    Technical background about this problem and related ones fixed at the same time can be found in the corresponding pull request #1111.