If you've used other testing frameworks, the concept of test or specification class setup and tear down will be familiar.
Spock provides a
setup method (we can use IntelliJ IDEA's code generation to create this), which will be run before every individual test method in this class. This can be used to set up a clean state at the start of each test.
To clean up data or state at the end of every test method, you need a
cleanup method. This is run after every individual test method.
setupSpec method to set up state once at the start of the specification, this is for things that should not change between individual test methods.
cleanupSpec method for final teardown code, this method will be called once at the very end of running all the tests.
One final piece of useful information. The tests in this tutorial created the "objects under test" in the test methods themselves. However, you might also want to create your test instance as a field in the test. You can use the @Subject annotation on the field to show that this is the object under test (you can use this annotation on local variables in the methods too). You can then reference this field in the test methods just as you'd expect in any Java or Groovy class.
private Polygon polygon = new Polygon(5)
Now we know how to set up, and clean up, our test classes and methods. Let's look at how Spock tests can function not only as tests, but as living documentation of the expected functionality of the application.