javaspockclasscastexceptionmaven-surefire-pluginspock-reports

'ClassCastException class [B cannot be cast to class [C' when attempting to generate spock reports


When generating unit test reports using spock-reports, I'm getting a ClassCastException:

class [B cannot be cast to class [C ([B and [C are in module java.base of loader 'bootstrap')

I'm using Java 11 with Spock 2.0 for unit tests and spock-reports (2.0.1-RC3) to generate test reports, initiated by surefire (2.22.2). I also use the spock collaborators (1.2.2) extension.

Although individual test reports are created successfully, when spock-reports tries to generate the aggregate HTML report (index.html), it gets:

c.a.s.r.internal.HtmlReportAggregator    : Failed to create aggregated report

java.lang.ClassCastException: class [B cannot be cast to class [C ([B and [C are in module java.base of loader 'bootstrap')
    at groovy.json.internal.FastStringUtils$StringImplementation$1.toCharArray(FastStringUtils.java:88) ~[groovy-all-2.3.8.jar:2.3.8]
    at groovy.json.internal.FastStringUtils.toCharArray(FastStringUtils.java:175) ~[groovy-all-2.3.8.jar:2.3.8]
    at groovy.json.internal.BaseJsonParser.parse(BaseJsonParser.java:103) ~[groovy-all-2.3.8.jar:2.3.8]
    at groovy.json.JsonSlurper.parseText(JsonSlurper.java:208) ~[groovy-all-2.3.8.jar:2.3.8]
    at groovy.json.JsonSlurper$parseText.call(Unknown Source) ~[na:na]
    at org.codehaus.groovy.runtime.callsite.CallSiteArray.defaultCall(CallSiteArray.java:47) ~[groovy-3.0.9.jar:3.0.9]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:125) ~[groovy-3.0.9.jar:3.0.9]
    at org.codehaus.groovy.runtime.callsite.AbstractCallSite.call(AbstractCallSite.java:139) ~[groovy-3.0.9.jar:3.0.9]
    at com.athaydes.spockframework.report.internal.ReportDataAggregator$_getAllAggregatedDataAndPersistLocalData_closure1.doCall(ReportDataAggregator.groovy:44) ~[spock-reports-2.3.0-groovy-3.0.jar:2.3.0-groovy-3.0]

I can see from the stacktrace that Spock is using groovy 3.0.9 but groovy-all 2.3.8 is being pulled in (by the spock-collaborators extension).

Although I can and will investigate updating the various dependency versions, is there anything I can do in the meantime to prevent this exception so that index.html can be generated?


Solution

  • System Property

    Aside from updating the other dependencies to later versions and aligning groovy versions, which ought to resolve it, a quick fix is to declare a system property:

    groovy.json.faststringutils.disable=true
    

    We can set this for our tests by adding it as a surefire systemPropertyVariables configuration entry in pom.xml:

    <plugin>
      <artifactId>maven-surefire-plugin</artifactId>
      <configuration>
        <systemPropertyVariables>
          <groovy.json.faststringutils.disable>true</groovy.json.faststringutils.disable>
        </systemPropertyVariables>
        ...
      ...
    </plugin>
    

    This was enough to get index.html generating successfully for us whilst I investigated uplifting dependency versions.

    Dependency Versions

    The alternative way of fixing is to uplift dependencies to the following versions (the ones you use):

    and add a direct groovy-all dependency (test scope, since we're only using groovy for our tests):

    <dependency>
        <groupId>org.codehaus.groovy</groupId>
        <artifactId>groovy-all</artifactId>
        <version>3.0.10</version>
        <scope>test</scope>
        <type>pom</type>
    </dependency>