This article needs additional citations for verification .(March 2015) |
| Spock | |
|---|---|
| Official logo of Spock Framework Project | |
| Original authors | Peter Niederwieser, Luke Daley |
| Developers | Leonard Brünings, Spock Framework Team |
| Stable release | 2.4 / December 11, 2025 [1] |
| Type | Test framework |
| License | Apache 2.0 |
| Website | spockframework |
| Repository | github |
Spock is a Java testing framework capable of handling the complete life cycle of a computer program. [2]
The tool was initially created in 2008 by Peter Niederwieser, a software engineer with Gradleware. A second Spock committer is Luke Daley (also with Gradleware), the creator of the popular Geb testing framework.
Spock is a testing, specification, and mocking framework (test engine).
The tests are written in the Groovy programming language and thus can be used for unit testing of both Groovy and Java code, and for creating functional tests. The syntax utilizes Groovy DSL and some BDD concepts, allowing for writing concise but maintainable code.
The framework supports data-driven and combinatorial testing. Mocking, stubbing, and interaction-based testing are also provided out of the box.
In Spock, test classes are called specifications. They extend the library Specification class and may contain
The code of feature methods is split into logical blocks using the Gherkin style.
The given and setup blocks (which provide the same functionality) are used for setting up the initial state.
The when blocks describe actions, then — the verifications of the action results, so each when/then pair encapsulates a test step.
The expect block mixes the functionality of the when blocks. This simplifies the test code for simple cases.
The state can be restored in the cleanup block. The additional and block has no own meaning and is used for finer splitting steps into substeps of the same nature.
The where and filter blocks allow specifying input and expected data. The values may be defined using the table syntax or as value streams. It allows the implementation of data-driven tests, including automatic calculation of Cartesian products.
All blocks except where and filter may have labels working as comments and visible in test reports.
Spock uses JUnit runner, and specifications can be run by various build tools and IDEs, e.g., IntelliJ IDEA.
The framework supports parallel execution, which is based on its implementation in the JUnit Platform. It can be used on both specification and feature levels and is configured via annotations.
Also, the tests can be integrated with the Spring TestContext Framework.
The Spock Reports Extension (by Renato Athaydes) can be used to generate Spock reports. The extension supports custom CSS and copying code blocks from the specifications, so the reader can see exactly what was executed during a test run.