Groovy for Domain-specific Languages - Second Edition

(nextflipdebug2) #1
Chapter 3

[ 43 ]

Spock specification structure


Spock specifications are written as a Groovy class that extends one of the core
Spock specification classes. We will use spock.lang.Specification for our
tests. The anatomy of the specification itself breaks down as follows:



  • Feature methods: These are the core of the Spock specification. They describe
    the feature of the system under test and how it is expected to work.

  • Blocks: Within a feature method, Spock specification comprise of blocks.
    These are the conceptual elements of the test and describe initial state and
    expected outcomes of a test specification.

  • Fields: Class instance fields are where we store objects belonging to a
    specifications fixture.

  • Fixture methods: These are the methods Spock uses for setup and
    cleanup operations.

  • Helper methods: Spock aims through its rich syntax to make specification as
    expressive as possible. Often, however, our specifications get large or contain
    duplication. Helper methods are not run as features by the Spock, but are
    used within the feature methods themselves.


Feature methods


The fundamental building block of a Spock test specification is the feature method.
Feature methods in a Spock specification are any method in the specification class
that contains Spock blocks, which we will describe in the next section. We can
declare a feature method using classic Java style method naming, or we can do so
using a special Groovy naming style, which allows method names as string.


The latter style is the convention in Spock, which supports the BDD goal of defining
tests in plain English. We can in fact chose whatever we like as a feature method
name, but the more descriptive we make these names, the more readable our test
specifications become. Once you make regular use of BDD, it starts to become natural
to define your tests in the same plain English used in the acceptance tests written in
your user stories.

Free download pdf