Spock Programmer : Setup / Cleanup Inheritance

Craig Atkinson‘s talk at GR8Conf US Intro to Spock and Geb claimed the next about Spock Cleanup / Inheritance.

  • Might have a hierarchy of test classes for DRY common test code
  • Spock runs the base class setup first
  • Then goes down the inheritance chain
  • cleanup is the reverse, starting at test class then going up to base class

I want it to double test to make sure I understood.

import spock.lang.Specification

abstract class BaseSpec extends Specification {
    def setup() { println 'base setup()' }
    def cleanup() { println 'base cleanup()' }

class DerivedSpec extends BaseSpec {
    def setup() { println 'derived setup()' }
    def cleanup() { println 'derived cleanup()' }

    def "test setup / cleanup inheritance"() {
        println "exectuting test case"

Running the above test prints:

base setup()
derived setup()
exectuting test case
derived cleanup()
base cleanup()

Really nice. Spock Setup / Cleanup inheritance to aid the creation of a hierarchy of tests classes.

Do you like to read about Geb development? Yes, then Subscribe to Groovy Calamari a weekly curated email newsletter about the Groovy ecosystem which I write 

Leave a Reply

Your email address will not be published. Required fields are marked *