Reference articles on history, science, culture and more
Encyclopedia

Spock (testing framework)

Image credit is listed at the end of this article.

Spock is a Java testing framework capable of handling the complete life cycle of a computer program.

01History

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.

02Features

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.

03Test code structure

In Spock, test classes are called specifications. They extend the library Specification class and may contain

  • fields (instance, static, and shared)
  • fixtures (fixture methods that set up and clean up the state)
  • features (feature methods)
  • helpers (helper methods)

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.

04Test execution

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.

05Reporting

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.

Watch videos about Spock (testing framework)Explainers and documentaries on YouTube (opens in a new tab)

Sources and credits

This article is adapted from the Wikipedia article Spock (testing framework), written by its contributors and licensed under CC BY-SA 4.0. Fathomly has changed the layout, removed citation markers, navigation and maintenance notices, and adjusted punctuation. This adapted version is shared under the same license. For references, see the original article.

Images, from Wikimedia Commons:

Fathomly is not affiliated with or endorsed by the Wikimedia Foundation. Spotted a problem? Tell us.