If we have any resource and we need to close it when current spec fails or done (for example, ZooKeeper client), the easiest way is to use the @AutoCleanup annotation with the resource:
@AutoCleanup private ZooKeeper zooKeeper;
By default, it calls the close() method, but you can put your own:
@AutoCleanup(value = "shutdownMethodName") private YourResource resource;
And optionally use quiet mode, when Spock Framework are going to ignore any exceptions while closing resource:
@AutoCleanup(value = "shutdownMethodName", quiet = true) private YourResource resource;
P.S.: this annotation can be useful for some workarounds, when you need to perform some action when spec fails.
If you still have any questions, feel free to ask me in the comments under this article, or write me on firstname.lastname@example.org.
If I saved your day, you can support me :)