4.3. Exploring the lifecycle of a Spock test

When you create a unit test to examine the behavior of a specific class, you’ll find yourself writing the same code over and over again in the given: block. This makes sense because several test methods have the same initial state and only a different trigger (the when: block). Instead of copying and pasting this behavior (and thus violating the DRY[6] principle), Spock offers you several facilities to extract common preconditions and post-conditions of tests in their own methods.

6

This acronym stands for don’t repeat yourself. See https://en.wikipedia.org/wiki/Don’t_repeat_yourself for more information.

4.3.1. Setup and cleanup of a feature

In the Spock test that examines your imaginary electronic ...

Get Java Testing with Spock now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.