Getting started with Spock
Spock is a testing framework for Java and Groovy code that you can use to automate your tests. IntelliJ IDEA supports full Spock integration as well as additional Spock features that you can enable via third-party plugins such as Spock Framework Enhancements , Spock ADB , and so on.
Create a project
To see how the Spock framework works, create a regular Gradle project.
Create a project for Spock
- Launch IntelliJ IDEA. If the Welcome screen opens, click New Project . Otherwise, from the main menu, select File | New | Project .
- From the list of suggestions, add and configure the following options:
- Name : add a name of the project you are creating. For example, spock-tutorial .
- Location : add a path to the location of your project
- Git repository : if you want to create and upload your project to Git, you can select this option.
- Languages : select a language for your project. Since Spock is used for testing Java and Groovy, you can leave the default Java option. You can always add the Groovy language to your project later on.
- Build System : select a build tool that you want to use in your project. For example, Gradle.
- JDK : configure the project SDK. To develop Java-based applications, you need a JDK (Java Development Kit). If the necessary JDK is already defined in IntelliJ IDEA, select it from the list of detected JDKs. If you don’t have the necessary JDK on your computer, select Download JDK . In the next dialog, specify the JDK vendor, version, and change the installation path if required.
- Gradle DSL : select the appropriate DSL for your project. Since with Spock you usually work with Java or Groovy, you can leave the suggested Gradle option.
If you need to configure any advanced settings, you can do so in the Advanced Settings section.
Set up dependencies
After a basic Gradle project is created, you need to add Spock dependencies instead of the added default JUnit 5 dependencies to the Gradle build file in order to write tests for Spock.
Add Spock dependencies
- Open the build.gradle file in the editor.
- Generate a new dependency.
- In the Dependencies tool window, search for the latest version of the Spock-core library.
- Select the needed item in the list of search results and click Add . The dependency is added to the build.gradle file. Add a Groovy dependency the same way as you did the Spock dependency, since Spock tests are written in Groovy. Use the 3.0.8 version.
- Make sure both dependencies declared as testImplementation and delete the other JUnit dependencies since you don’t need them. You should see the following code in your build.gradle file:
Write a simple assertion
Before you start writing your test, make sure you’ve created a groovy folder in your project since you have created a project with mixed Java and Groovy languages. This way you can keep your Groovy code in the groovy folder and keep Java code in its java folder.
Add a Groovy folder
- In the Project tool window, right-click the test folder and select New | Directory .
- In the window that opens, IntelliJ IDEA displays the groovy option in the list because the Groovy plugin is used in Gradle. Select groovy and press Enter . IntelliJ IDEA adds the groovy directory to the test module and you can start writing tests.
Write a test for Spock
You can start by writing a very simple test, so you can see what Spock tests look like.
- In the Project tool window, right-click the groovy folder and select New | Groovy Class .
- In the window that opens, create the package and directory structure by typing the full package name before the class name. A Spock test is often called Specification , so you can call the test ExampleSpecification .
- After adding the Groovy class, open it in the editor.
- Make sure this class extends Spock’s Specification class.
- Inside the class, press Alt+Insert and select Test Method to generate test methods for the Spock specification. This can be especially useful when you are not so familiar with writing Spock or Groovy code, as IntelliJ IDEA will generate the basic structure that you need. Check the following code:
For more detailed information on writing Spock tests and tips on using Groovy code, check out the Spock blog post.
Run Spock tests
You can run the created test using the editor gutter or the main menu.
Run a Spock test
- In the editor, double-click in the gutter. Alternatively, you can press Shift+F10 .
- Check the results in the Run tool window. As you see, the test passed. You can change the code, so the test fails.
By default, IntelliJ IDEA uses Gradle to run tests if it’s a Gradle project. That means the IDE uses the same process to run tests as the continuous integration or build environment.
However, with such simple code in the project, the Gradle build isn’t doing anything extra like generating code or resources. In this case, it can be faster to use the IntelliJ IDEA test runner.
Change the test runner
- Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Build Tools | Gradle .
- From the options on the right, select IntelliJ IDEA in the Run Test using drop-down list.
- Click OK to save the changes. Now, when you run your tests, IntelliJ IDEA will use its native test runner.
Writing Tests with Spock
Groovy’s syntax and functionality might be unusual for Java developers to begin with, but it can be very helpful for testing.
Tests don’t check just the happy paths, sometimes we want to say we’re expecting a specific Exception.
Often we want to test the same set of criteria with different sets of data. Data pipes is one mechanism to do this.
Data tables offer the same functionality as data pipes, but this syntax can sometimes be more readable for more complex sets of data.
Mocks are a useful testing tool. We can provide «empty» objects, so we don’t have to initialise the whole application to test a section, and we can use mocks to check our code is making the calls we expect.
Stub objects allow us to state from a test what values get injected into our application code. They can provide simple interfaces, so we don’t have to initialise the whole application to test a section.
Sometimes we need to move test code into a separate method. In this section, we look at some tips for this.