Writing Tests with Spock
Learn about the Spock testing framework by creating a project that uses Spock to unit test Java code.
![Introduction](/guide/assets/thumbnail-8fa3c240.png)
Introduction
Introduction to the tutorial and links to further reading.
![Creating a Project for Spock](/guide/assets/thumbnail-2836dd2e.png)
Creating a Project for Spock
Start by creating a new Java project which will be the basis for the rest of this tutorial.
![Setting up the Dependencies](/guide/assets/thumbnail-ace4e675.png)
Setting up the Dependencies
Add Spock framework dependencies to the project.
![A Simple Assertion](/guide/assets/thumbnail-9964c396.png)
A Simple Assertion
Writing our first Spock test.
![Given, When and Then](/guide/assets/thumbnail-9b827136.png)
Given, When and Then
See how Spock tests are usually structured.
![Groovy Tips for Java Developers](/guide/assets/thumbnail-dd209e62.png)
Groovy Tips for Java Developers
Groovy's syntax and functionality might be unusual for Java developers to begin with, but it can be very helpful for testing.
![Label Flexibility](/guide/assets/thumbnail-5531030f.png)
Label Flexibility
Spock supports a range of different test labels to help you write the most readable tests.
![Spock IntelliJ IDEA Plugin](/guide/assets/thumbnail-d71a629b.png)
Spock IntelliJ IDEA Plugin
Add the Spock plugin to IntelliJ IDEA to get some extra help in the IDE.
![Expecting Exceptions](/guide/assets/thumbnail-8d435536.png)
Expecting Exceptions
Tests don't check just the happy paths, sometimes we want to say we're expecting a specific Exception.
![Data Pipes](/guide/assets/thumbnail-d347b433.png)
Data Pipes
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](/guide/assets/thumbnail-f61b3db4.png)
Data Tables
Data tables offer the same functionality as data pipes, but this syntax can sometimes be more readable for more complex sets of data.
![Mocks](/guide/assets/thumbnail-7e2e34bc.png)
Mocks
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.
![Stubs](/guide/assets/thumbnail-0e47cbf3.png)
Stubs
Stub can provide simple interfaces, so we don't have to initialise the whole application to test a section.
![Helper Methods](/guide/assets/thumbnail-2413e3cb.png)
Helper Methods
Sometimes we need to move test code into a separate method. In this section, we look at some tips for this.
![With](/guide/assets/thumbnail-8cb398a8.png)
With
Sometimes we want to check more than one value on an object. We can use `with` to do this.
![Verify All](/guide/assets/thumbnail-4ad3cc4d.png)
Verify All
Test frameworks often stop the test at the first failure. Find out how to run all conditions to see which pass and which fail.
![Setup and Teardown](/guide/assets/thumbnail-ae10ba57.png)
Setup and Teardown
See how to set up, or clean up, test classes and methods in Spock.
![Specifications as Documentation](/guide/assets/thumbnail-577ebabf.png)
Specifications as Documentation
Spock tests don't just tell the computer how to test the application, they can help developers understand what the application is supposed to do.
![Shortcuts used](/guide/assets/thumbnail-64f89d92.png)
Shortcuts used
Finally, let's summarise the shortcuts we've learnt and see links to further information.