Migrating to Spock 2.0 M1 with JUnit 5 – Gradle & Maven Setup Guide
This article explains the major changes in Spock 2.0 M1, how to integrate it with JUnit 5 via the JUnit Platform, provides step‑by‑step Gradle and Maven configurations, discusses compatibility issues with Groovy versions, and offers solutions for JUnit 4 Rule migration.
Spock 2.0 M1 Overview
Spock 2.0 M1 is a major upgrade built on JUnit 5 and Groovy 3 (which requires JDK 9+). It is a pre‑release version intended to gather feedback on the migration to the JUnit Platform; it is not recommended for production projects.
JUnit Platform Support
The core change is the migration to JUnit 5 via JUnit Platform 1.5. Tests written with Spock are automatically discovered and executed by any tool that supports the JUnit Platform (IDE, build tools, CI, etc.). Platform features also apply to Spock tests.
Adding Spock 2 to a Gradle Project
Update the Spock dependency:
testImplementation('org.spockframework:spock-core:2.0-M1-groovy-2.5')Activate the JUnit Platform in the test task:
test {
useJUnitPlatform()
}Adding Spock 2 to a Maven Project
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-core</artifactId>
<version>2.0-M1-groovy-2.5</version>
<scope>test</scope>
</dependency>If the junit-platform-engine transitive dependency is present, the Surefire plugin (version 3.0.0+) will run the tests on the JUnit Platform by default.
Other Changes
Because of the JUnit Platform migration, the number of additional changes in Spock 2.0 M1 is limited, making regression hunting easier. The required Java version is now 8. All parameterized tests run automatically, but the old spock-global-unroll feature is not yet supported.
Some extensions such as SpockReportingExtension are temporarily disabled.
JUnit 4 Rule Issues
Tests that use JUnit 4 @Rule or @ClassRule may fail with NullPointerException or IllegalStateException because the JUnit Platform no longer supports the Rules API. To ease migration, Spock provides the spock-junit4 extension, which wraps JUnit 4 rules as Spock extensions.
Add the dependency:
testImplementation 'org.spockframework:spock-junit4:2.0-M1-groovy-2.5'Or in Maven:
<dependency>
<groupId>org.spockframework</groupId>
<artifactId>spock-junit4</artifactId>
<version>2.0-M1-groovy-2.5</version>
<scope>test</scope>
</dependency>Other Known Issues
Spock 2.0 M1 compiles and runs only with Groovy 2.5.8. Attempts to use Groovy 3.0 result in a hidden error such as:
Could not instantiate global transform class org.spockframework.compiler.SpockTransform ... because of exception java.lang.reflect.InvocationTargetExceptionThis limitation reduces the ability to run Spock tests that depend on Groovy 3, especially in edge cases. Future releases may add compatibility or remove the restriction.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
