IntelliJ IDEA 2022.2 Help

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

  1. Launch IntelliJ IDEA.

    If the Welcome screen opens, click New Project.

    Otherwise, from the main menu, select File | New | Project.

    Spock new project
  2. 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.

  3. After you finish selecting necessary options, click Create.

    IntelliJ IDEA creates a Gradle project, downloads the necessary dependencies, and sets up the appropriate project structure.

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

  1. Open the build.gradle file in the editor.

  2. Generate a new dependency.

  3. In the Dependencies tool window, search for the latest version of the Spock-core library.

    Search for Spock dependency
  4. 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.

    Search for Groovy dependency
  5. 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:

    dependencies { testImplementation 'org.codehaus.groovy:groovy-all:3.0.8' testImplementation 'org.spockframework:spock-core:2.0-groovy-3.0' }
  6. Add the Groovy plugin to the build.gradle file:

    plugins { id 'java' id 'groovy' }
  7. Load all your changes that you made to the build.gradle file. Click the Load Gradle Changes icon in the editor or press Ctrl+Shift+O.

    IntelliJ IDEA downloads any new dependencies, removes the old ones, and correctly configures the project.

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

  1. In the Project tool window, right-click the test folder and select New | Directory.

  2. In the window that opens, IntelliJ IDEA displays the groovy option in the list because the Groovy plugin is used in Gradle.

    Add a Groovy test folder

    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.

  1. In the Project tool window, right-click the groovy folder and select New | Groovy Class.

  2. 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.

  3. After adding the Groovy class, open it in the editor.

  4. Make sure this class extends Spock’s Specification class.

    Extend Spock test class
  5. Inside the class, press Alt+Insert and select Test Method to generate test methods for the Spock specification.

    Generated Test method

    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:

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

    As you see, the method is defined with the def keyword, and Spock’s method names can be Strings. This is helpful when you create tests.The text format gives you flexibility for describing the exact behavior you expect.

  6. Spock tests don’t use an annotation or a specific test method name format to define which methods are tests. Instead, they use labels. For such simple tests, you can use expect and define the simplest behavior that you expect to see.

    Check the following example:

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

    As you see, Spock also doesn't use Assertions or the 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.

  7. 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.

    To see the test fail, use the following code and run your test:

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

    Check the result and the error message in the Run tool window.

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

  1. In the editor, double-click the Run button in the gutter.

    Alternatively, you can press Shift+F10.

  2. Check the results in the Run tool window.

    As you see, the test passed.

    Run tool window / Spock test output

    You can change the code, so the test fails.

    Spock test failed

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

  1. Press Ctrl+Alt+S to open the IDE settings and select Build, Execution, Deployment | Build Tools | Gradle.

  2. From the options on the right, select IntelliJ IDEA in the Run Test using drop-down list.

  3. Click OK to save the changes.

    Now, when you run your tests, IntelliJ IDEA will use its native test runner.

Last modified: 14 February 2022