Spock is an awesome test framework for testing our Java or Groovy code. Spock itself is written with Groovy and provides a nice syntax to define our tests, or specifications in Spock terminology. To configure support for using Spock in our Gradle build is very easy with the JVM Test Suite plugin (included with the Java plugin). The plugin gives us a nice syntax to define different types of tests, for example integration tests, with their own source set, dependencies and configuration. To use Spock as testing framework we only have to use the method useSpock
within a test configuration. The default version of Spock that is used is 2.1-groovy-3.0 when we use Gradle 7.6. If we want to use another version we can use a String
parameter when we use the useSpock
method with the version we want to use.
In the following example we use the default Spock version defined by Gradle 7.6:
// File: build.gradle.kts plugins { // We need the Groovy plugin to run our // Spock specifications. // As it includes the Java plugin it also // includes the JVM Test Suite plugin. groovy } repositories { // We need a repository with the Spock dependencies. mavenCentral() } testing { suites { val test by getting(JvmTestSuite::class) { // Define we want to use Spock. useSpock() } } }
When we check the dependencies in the testCompileClasspath configuration we see the following output:
$ gradle dependencies --configuration testCompileClasspath ... > Task :dependencies ------------------------------------------------------------ Root project 'spock-testsuite' - Sample project for using Spock ------------------------------------------------------------ testCompileClasspath - Compile classpath for source set 'test'. \--- org.spockframework:spock-core:2.1-groovy-3.0 +--- org.codehaus.groovy:groovy:3.0.9 +--- org.junit:junit-bom:5.8.1 | +--- org.junit.platform:junit-platform-engine:1.8.1 (c) | \--- org.junit.platform:junit-platform-commons:1.8.1 (c) +--- org.junit.platform:junit-platform-engine -> 1.8.1 | +--- org.junit:junit-bom:5.8.1 (*) | +--- org.opentest4j:opentest4j:1.2.0 | +--- org.junit.platform:junit-platform-commons:1.8.1 | | +--- org.junit:junit-bom:5.8.1 (*) | | \--- org.apiguardian:apiguardian-api:1.1.2 | \--- org.apiguardian:apiguardian-api:1.1.2 \--- org.hamcrest:hamcrest:2.2 (c) - dependency constraint (*) - dependencies omitted (listed previously) ... $
We can also specify another version we want to use. In the following example we refer to a version in the version catalog when we use the useSpock
method:
# File: gradle/libs.versions.toml [versions] # We define the Spock version in the version catalog. spock = "2.3-groovy-4.0"
// File: build.gradle.kts plugins { // We need the Groovy plugin to run our // Spock specifications. // As it includes the Java plugin it also // includes the JVM Test Suite plugin. groovy } repositories { // We need a repository with the Spock dependencies. mavenCentral() } testing { suites { val test by getting(JvmTestSuite::class) { // Define we want to use Spock // and specify a non-default version // using version catalog. useSpock(libs.versions.spock) } } }
Let's check the testCompileClasspath
configuration again and we see this time our Spock version is 2.3-groovy-4:
$ gradle dependencies --configuration testCompileClasspath ... > Task :dependencies ------------------------------------------------------------ Root project 'spock-testsuite' - Sample project for using Spock ------------------------------------------------------------ testCompileClasspath - Compile classpath for source set 'test'. \--- org.spockframework:spock-core:2.3-groovy-4.0 +--- org.apache.groovy:groovy:4.0.4 | \--- org.apache.groovy:groovy-bom:4.0.4 | \--- org.apache.groovy:groovy:4.0.4 (c) +--- org.junit:junit-bom:5.9.0 | +--- org.junit.platform:junit-platform-engine:1.9.0 (c) | \--- org.junit.platform:junit-platform-commons:1.9.0 (c) +--- org.junit.platform:junit-platform-engine -> 1.9.0 | +--- org.junit:junit-bom:5.9.0 (*) | +--- org.opentest4j:opentest4j:1.2.0 | +--- org.junit.platform:junit-platform-commons:1.9.0 | | +--- org.junit:junit-bom:5.9.0 (*) | | \--- org.apiguardian:apiguardian-api:1.1.2 | \--- org.apiguardian:apiguardian-api:1.1.2 \--- org.hamcrest:hamcrest:2.2 (c) - dependency constraint (*) - dependencies omitted (listed previously) ... $
Written with Gradle 7.6.