Spock auto-cleanup of resources
What's inside this article
⌄
- How to close resources in Spock
- Spock auto cleanup resources
- Spock resource cleanup
- Spock cleanup annotation
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.