IntelliJ IDEA Tutorials Videos

Tutorial: Spock Part 1 – Getting started

Spock is a really interesting framework for writing automated tests on the JVM. Spock tests are written in Groovy, but they can be used to test Java classes as well. Check out our 30 minute video tutorial on how to write tests using Spock, or follow along with this series of blog posts, which contains code samples and links to further information. Or do both!

In this post, Part 1 in the series, we’re going to cover just enough to get you started with a brand new project for writing Spock tests.

Tutorial: Spock

These blog posts cover the same material as the video. This provides an easy way for people to skim the content quickly if they prefer reading to watching, and to give the reader/watcher code samples and links to additional information.

Creating a Project for Spock

Let’s start by creating a new project that’s going to contain Java project code, and use Spock to unit test that code.

From the new project window, we can choose any of:

  • Java
  • Maven
  • Gradle

…from the left-hand options to create our JVM project. For this tutorial we’re going to use Gradle as our build tool.

Spock is a Groovy testing framework, although we’re going to use it to test Java code, so we could select Groovy as an additional library to add right at the start. However, we can also add Groovy later on, which is what I want to show, so I’m not going to select it here.

We’re using JDK 11 for this project, but there’s no functionality here that requires 11, you can use whichever version you’re comfortable with here.

Call the project spock-tutorial, and save it to some useful location.

IntelliJ IDEA creates the files for the project and initialises the structure of the project. It’s using Gradle to build this skeleton project to make sure all dependencies are downloaded and set up.

Setting Up Dependencies

Now the basics of the project have been created, let’s take a closer look and set up the dependencies we need.

IntelliJ IDEA adds JUnit 5 as the default testing framework, which is a logical choice, but we want to use Spock instead of JUnit. We can generate a new dependency for the libraries we want using ⌘N / Alt+Insert in the build.gradle file and then choosing “Add Maven artifact dependency”.

We can search for org.spockframework in the artifact search, and add the latest version of the spock-core library. This is currently Spock 2.0 for Groovy 3. This is a test dependency so make sure the declaration is testImplementation.

We also need a dependency on Groovy, since Spock tests are written in Groovy. Follow the same steps for adding a new Maven artifact dependency, search for Groovy and add a dependency on Groovy 3.0.8. For now, we’re only going to use Groovy for testing, so we’ll declare this as testImplementation too.

You can delete the other JUnit dependencies since we don’t need them, your build.gradle dependencies should look like:

dependencies {
    testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0'
    testImplementation 'org.codehaus.groovy:groovy-all:3.0.8'
}

Since we’re using Groovy, we need to add the groovy plugin to Gradle. When we’re using the Groovy plugin, we don’t need to specifically add the Java plugin as well, since all that functionality is included in the Groovy plugin. We might want to delete java plugin just to keep the noise down in our build file. We can keep the plugin to be explicit, or delete it to be more succinct, it’s really down to our own preferences.

plugins {
    id 'groovy'
}

Full build.gradle code

Now we can load all the Gradle changes with ⇧⌘I (macOS), or Ctrl+Shift+O (Windows/Linux), and IntelliJ IDEA will download any new dependencies, remove the old ones, and correctly configure the project.

With a mixed language project like this one, one way to organise the files is to keep Java code in the java folder and to create a groovy folder for Groovy code.

From the Project window, select the test folder and press ⌘N (macOS) or Alt+Insert (Windows/Linux). Select “Directory”, and start typing “groovy” in the “New Directory” dialog. IntelliJ IDEA should suggest “groovy” in the dropdown because we’re using the Groovy plugin in Gradle.

View steps in video

A Simple Assertion

Let’s start by writing a very simple test, so we can see what Spock tests look like.

From the Project window, we can use the shortcut ⌘N (macOS) or Alt+Insert (Windows/Linux) to create a new file. Create a Groovy class, since Spock tests are Groovy classes:

Create the package and directory structure by typing the full package name before the class name.

Spock tests are often called Specifications, so call the test ExampleSpecification. Make sure this class extends Spock’s Specification class. We can get IntelliJ IDEA to generate test methods for Spock specifications using ⌘N (macOS) or Alt+Insert (Windows/Linux) inside the class.

This can be especially useful when we’re not so familiar with writing Spock or groovy code, as IntelliJ IDEA will generate the basic structure that we need. The method is defined with the “def” keyword, and Spock’s method names can be Strings. This is extremely useful when we’re creating tests, as the text format gives us a lot of flexibility for describing exactly what we are specifying the behaviour should be.

class ExampleSpecification extends Specification {
    def "should be a simple assertion"() {

    }
}

Spock tests don’t use an annotation or a specific test method name format to define which methods are tests. Instead, they use labels. We’ll see the different types of labels available to use throughout the tutorial, for this simplest test we’re going to use expect. This can be used for defining the simplest behaviour we expect to see.

def "should be a simple assertion"() {
    expect:
    1 == 1
}

Spock also doesn’t use Assertions or an Assert keyword, at least not normally. Instead, you can use simple checks, like the double equals. The should be a simple assertion method specifies a simple expected behaviour – that the number 1 should be equal to 1. It’s not a realistic test case, but it shows the basics of a Spock test.

Run this test using ⌃R (macOS) or Shift+F10 (Windows/Linux), or the green arrow in the gutter near the line numbers. This test should pass.

Now, change it so that it fails, it’s always helpful to see a test fail first to make sure it’s actually working and testing the right things. When a test fails, Spock displays a helpful error message which shows exactly what failed and why.

Condition not satisfied:

1 == 0
  |
  false

By default IntelliJ IDEA uses Gradle to run tests if it’s a Gradle project. This is normally what we want, as it means our IDE is using the same process to run tests as the continuous integration or build environment is using. In a simple project like this one, where the Gradle build isn’t doing anything extra like generating code or resources, it can be faster to use the IntelliJ IDEA test runner. We can find this in the preferences under Build, Execution, Deployment > Gradle, and we can select IntelliJ IDEA to run the tests.

Fix this test and rerun it to make sure it passes. You should see it’s not using Gradle to run it, there shouldn’t be any Gradle output in the test results window.

View steps in video

Conclusion

In this blog, we covered the first three steps in our Spock tutorials. Now you know how to:

  • Set up your project to use Spock
  • Create simple a Spock test

Spock has much more to offer than this, stay tuned for further blog posts, watch the full video, or take a look at the excellent reference documentation.

image description