The growing popularity of BDD (Behavior Driven Development) makes that more and more developers decide to use Spock Framework for their tests. If you haven’t had a chance to familiarize yourself with this testing framework yet, I encourage you to do so, because in some respects it is much better than the standard JUnit 5. Perhaps these differences will make Spock perfect for your needs. In this article, I will focus only on the configuration aspect of the maven and Spock project in several variants – minimal, with the unmodified name of the test classes and the Spock configuration for SpringBoot.
1. Maven Spock configuration in pom.xml
The minimal configuration of Spock means basically just add Spock Framework dependencies to the project, and to use two plugins when building a project. In the compleTest phase, use the gmavenplus-plugin plugin, which will compile groovy test classes and place the result in target/test-classes. For the tests to run, we need to configure the maven-surefire-plugin plugin.
Warning! In the default configuration, maven-surefire-plugin assumes that your tests will have the suffix “Test”, so it is important to name your test class eg DummySpecTest.groovy. Below is a sample pom.xml file with the minimum configuration. Important lines were highlighted.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>Spock2xExample</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-core</artifactId> <version>2.0-M3-groovy-3.0</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>11</release> </configuration> </plugin> <plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <version>1.11.0</version> <executions> <execution> <goals> <goal>compileTests</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> </plugin> </plugins> </build> </project>
After refreshing the pom.xml configuration, create the test/groovy folder. Some IDEs such as InteliJ idea should recognize it as a groovy class folder. If you have trouble with this, try reimporting the maven project. Below is a sample test and directory structure that may be helpful.

package com.bettercoding.spock import spock.lang.Specification class DummySpecTest extends Specification { def "This is my first test in Spock"() { given: def x = 1 when: x = 2 then: x == 1 //The test should fail here } }
2. Configure test classes names
By defaut, maven-surefire-plugin runs tests classes matched to the patterns.
"**/Test*.java" - includes all of its subdirectories and all Java filenames that start with "Test". "**/*Test.java" - includes all of its subdirectories and all Java filenames that end with "Test". "**/*Tests.java" - includes all of its subdirectories and all Java filenames that end with "Tests". "**/*TestCase.java" - includes all of its subdirectories and all Java filenames that end with "TestCase".
If you want your groovy classes to be named differently, eg DummyIntegrationSpec, then you can add the appropriate configuration to pom.xml. If you are interested in this topic exactly, I refer you to the official documentation.
3. Spock and Spring Framework configuration (SpringBoot)
The configuration of Spock running with Spring, or in fact SpringBoot, differs in a few details:
- pom.xml contains spring-boot configuration
- Use spock-spring dependency instead of spock-core
- At the moment SpringBoot does not support Groovy 3.0, so be sure to choose the version of Spock marked with groovy-2.5
- The test should be marked with an annotation @SpringBootTest
The pom.xml file below contains the complete SpringBoot configuration along with the Spock testing framework. I have highlighted the mentioned details that make it different from the configuration without SpringBoot.
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.example</groupId> <artifactId>Spock2xExample</artifactId> <version>1.0-SNAPSHOT</version> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>2.3.4.RELEASE</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency> <dependency> <groupId>org.spockframework</groupId> <artifactId>spock-spring</artifactId> <version>2.0-M3-groovy-2.5</version> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.1</version> <configuration> <release>11</release> </configuration> </plugin> <plugin> <groupId>org.codehaus.gmavenplus</groupId> <artifactId>gmavenplus-plugin</artifactId> <version>1.11.0</version> <executions> <execution> <goals> <goal>compileTests</goal> </goals> </execution> </executions> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <version>3.0.0-M5</version> </plugin> </plugins> </build> </project>
package com.bettercoding.spock import org.springframework.beans.factory.annotation.Autowired import org.springframework.boot.test.context.SpringBootTest import org.springframework.context.ApplicationContext import spock.lang.Specification @SpringBootTest class DummySpecTest extends Specification { @Autowired private ApplicationContext applicationContext def "Ckeck Spring application stared successfully"() { when: true then: applicationContext != null } }
At the end… May I ask you for something?
If I helped you solve your problem, please share this post. Thanks to this, I will have the opportunity to reach a wider group of readers. Thank You