Using Spock Framework In a Spring Maven Java Project

“Spock is a testing and specification framework for Java and Groovy applications. What makes it stand out from the crowd is its beautiful and highly expressive specification language.” (From the framework’s documentation –

To use it in Spring Maven Java Project, do the following:

1. Add Necessary Dependencies/Plugins to Your Pom

SpockFramework Dependency


Groovy Dependency


GmavenPlus Plugin
The GmavenPlus plugin is used in compiling groovy code. Since spock tests are in Groovy, we will need the GmavenPlus plugin. For more on using the GmavenPlus plugin, see


The GmavenPlus plugin goals are tied to the needed phases of the Maven Life Cycle. mvn compile will, therefore, also compile the Groovy tests and mvn tests will also run the groovy tests.

2. Write Your Spock Tests (Specifications)

Place your Groovy tests in src/test/groovy.

Many tutorials on writing spock specifications can be found online. Here’s an example of a spock specification:

import spock.lang.Specification;

public class TrySpockTest extends Specification {
  void "trying Spock Test"() {
    int x = 5
    int y = 7
    x + y == 12

You can test a Java object in your Test class by simply importing it, creating an instance and testing it.

3. Run your Tests
Run mvn tests to test all classes including Groovy tests.

Run Your Spock Tests in STS (Eclipse) or Any IDE:
When writing tests, you do not want to have to build your project (i.e run mvn tests or whatever) each time you make a change. You want to be able to make a change in your test or code then Run the test just like you do with JUnit tests.

The plugin for Groovy in Eclipse (Groovy Eclipse Feature) has JUnit support and so allowed me to run my Spock specification like a regular Junit test in STS IDE. However, I found that when I edited a test and re-run the changes were not reflected. I changed the output folder for src/test/groovy which contains my groovy test classes to ‘target/test-classes’ but that did not do the trick. I still found the Groovy Eclipse Feature useful because it added Groovy support to STS (syntax highlighting, code completion in some cases among others) so it was still cool.

I needed another way of running the tests in STS though so I used a Junit Test Suite which you should be able to use with any IDE with JUnit support.

import org.junit.runners.Suite.SuiteClasses
import org.junit.runners.Suite
import org.junit.runner.RunWith
import spock.lang.Specification

@SuiteClasses([TrySpockTest, TrySpockTest2])
public class MyTestSuite {


TrySpockTest and TrySpockTest2 are the names of the spock specifications in src/test/groovy that I want to run from my IDE. MyTestSuite is in the same src/test/groovy folder.


One thought on “Using Spock Framework In a Spring Maven Java Project

  1. What you have under execution goals for gmavenplus-plugin is mostly correct. Or I should say, needs to be trimmed unless your app also includes groovy code.

    If your app is a pure Java app and ONLY contains Spock specs for testing only, then the only execution goals you need are:


    So it might be something you want to add to your explanation. You only need the other goals if and ONLY *IF* your app code (not Specs/tests) includes groovy code.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s