When we write a feature method in our Spock specification to test our class we might run into long running methods that are invoked.
We can specify a maximum time we want to wait for a method.
If the time spent by the method is more than the maximum time our feature method must fail.
Spock has the
@Timeout annotation to define this.
We can apply the annotation to our specification or to feature methods in the specification.
We specify the timeout value as argument for the
Seconds are the default time unit that is used.
If we want to specify a different time unit we can use the annotation argument
unit and use constants from
java.util.concurrent.TimeUnit to set a value.
In the following example specification we set a general timeout of 1 second for the whole specification. For two methods we override this default timeout with their own value and unit: