I prefer to use Spock for my tests, but it doesn’t play nice with the latest Java versions. The latest release version of Spock runs fine on Java 8, but every newer version prints a bunch of warnings, as shown below.
WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.reflection.CachedClass (file:/C:/Users/Bouke/.m2/repository/org/codehaus/groovy/groovy-all/2.4.15/groovy-all-2.4.15.jar) to method java.lang.Object.finalize()
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.reflection.CachedClass
WARNING: Use –illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release
Furthermore Spock 2.0 is in the works for some time and there has been a milestone release in the beginning of January 2020. So I decided to kill two birds with one stone. I went looking for a solution that combined the use of Spock 2.0 and that got rid of the warnings. The following solution is tested with Java 14 (and below).
Basically the solution consists of the following three components:
To see the three components working together have a look at this pom file:
So let’s explain all relevant sections in this pom file.
<plugin> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>14</source> <target>14</target> </configuration> </plugin>
We have to specify some configuration for the maven-compiler-plugin. Here you set the Java version you would like to use. In this case we use Java 14. Please make sure that you use a Java version at least as high as the one that you specify here. So for this example you will need to run it with Java 14 or higher.
<plugin> <artifactId>maven-surefire-plugin</artifactId> <version>2.22.0</version> </plugin>
You need a newer version of the surefire plugin otherwise the test runner will not find your tests.
<dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>2.0-M1-groovy-2.5</version> <scope>test</scope> </dependency>
Use the milestone release of Spock 2.0.
<dependency> <groupId>org.codehaus.groovy</groupId> <artifactId>groovy</artifactId> <version>3.0.0-rc-2</version> <scope>test</scope> </dependency>
Use the latest version of Groovy which does support the latest Java versions. And so the warnings disappear!
I hope you found this useful and thanks for reading! 🖖