Reputation: 1373
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?
Upvotes: 1
Views: 510
Reputation: 426
To share some background information on this issue. JSONSlurper until Groovy 2.4.5 used a String implementation with an array of chars. This worked with Java 8, but since Java 9 the String implementation uses an array of bytes, containing either a 8 bits representation or a 16 bits representation, instead of an array of chars.
In Groovy 2.4.6 this was solved, thus this or a newer versions should be good to avoid the error:
class [B cannot be cast to class [C ([B and [C are in module java.base of loader 'bootstrap')
This issue occured in the groovy json library, that is also used within the groovy-all library. So upgrading this dependency or the groovy-all dependency solves the issue.
When you are using a newer version of Groovy (2.4.6+), and you still get this error, then it's often a problem with dependency resolution. For example I used Groovy 4, but one of my dependencies Smooks-all 1.7.1 still used Groovy 2.4.4 under the hood. This couldn't get resolved because the old Groovy version still uses the "org.codehaus.groovy" artifact, while newer Groovy versions use the "org.apache.groovy" artifact.
To find out if your project still uses an old Groovy version, you can run Maven dependency tree or search for the older Groovy dependency in your local Maven repository, delete it and see what Maven module is downloading the old Groovy dependency again. After that you either exlcude the old Groovy dependency or upgrade the library to use a newer version of Groovy.
Upvotes: 1
Reputation: 1373
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.
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>
Upvotes: 1