Chapter 1. The “Up and Running” Part

Installation

Spock is available from the Maven Central and JCenter repositories. As of this writing, the current version is 1.1.

There are different versions for compatibility with Groovy 2.0+, 2.3+, and 2.4+. If you’re not using Groovy in the production code of your project there’s no reason to use anything other than the newest version of Groovy. Groovy need only be a test dependency of your project.

Running with the JUnit Runner

Spock is compatible with JUnit’s test runner. It should be possible to run Spock specifications anywhere you can run JUnit tests; that means with IDEs like IntelliJ Idea and Eclipse and command-line build tools such as Gradle and Maven.

Running with Gradle

To include Spock in a project built with Gradle, you simply need to include the spock-core library, as follows:

apply plugin: "groovy" 1

repositories {
  jcenter() 2
}

dependencies {
  testCompile "org.spockframework:spock-core:1.1-groovy-2.4" 3
}
1

Include the Groovy Gradle plugin

2

Include the JCenter repository (or mavenCentral)

3

Include Spock on the testCompile classpath

With this setup, neither Spock nor Groovy will be included in the production artifacts built by Gradle.

When Groovy Is Used in the Project

If your project does use Groovy in its production code, you can simply add Groovy to the main classpath and Spock to the test classpath, as demonstrated here:

apply plugin: "groovy"

repositories {
  jcenter()
}

dependencies {
  compile "org.codehaus.groovy:groovy-all:2.4.8"
  testCompile "org.spockframework:spock-core:1.1-groovy-2.4"
}

Spock will transitively include Groovy, but the version will be synchronized with whatever version you include in the main classpath.

Synchronizing Groovy Versions Between Main and Test Classpaths

If, and only if, your main project includes the groovy dependency rather than the groovy-all dependency, you should explicitly synchronize the version of Spock’s transitive dependency, like so:

dependencies {
  compile "org.codehaus.groovy:groovy:2.4.8" 1
  testCompile "org.spockframework:spock-core:1.1-groovy-2.4"
}

configurations.all {
  resolutionStrategy {
    force "org.codehaus.groovy:groovy-all:2.4.8" 2
  }
}
1

The compile classpath includes Groovy but not using the groovy-all artifact on which Spock depends.

2

We use a resolutionStrategy to ensure Spock pulls in a compatible version of groovy-all.

Running with Maven

Maven setup is a little more complex than Gradle because Maven does not natively know how to compile Groovy code. It’s still fairly straightforward, though:

  <build>
    <plugins>

      <plugin>
        <groupId>org.codehaus.gmavenplus</groupId>
        <artifactId>gmavenplus-plugin</artifactId>
        <version>1.5</version>
        <executions>
          <execution>
            <goals>
              <goal>testCompile</goal>
            </goals>
          </execution>
        </executions>
      </plugin>

      <plugin>
        <artifactId>maven-surefire-plugin</artifactId>
        <version>2.18.1</version>
        <configuration>
          <useFile>false</useFile>
          <includes>
            <include>**/*Spec.java</include>
          </includes>
        </configuration>
      </plugin>

    </plugins>
  </build>

  <dependencies>

    <dependency>
      <groupId>org.spockframework</groupId>
      <artifactId>spock-core</artifactId>
      <version>1.1-groovy-2.4</version>
      <scope>test</scope>
    </dependency>

  </dependencies>
  <!--tag::maven[]-->
</project>

The gmavenplus plugin enables Maven to compile Groovy code. In the example, I’ve included it only for the testCompile goal.

The Surefire plugin is only required to ensure Maven’s test runner executes files ending in *Spec.

Get Spock: Up and Running now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.