The verify
method should accept a Closure
(code block) containing the assertion, execute it, and store any exception thrown. Usage then looks as follows: verify { assert 8 == (3 + x) * z }
.
If you annotate the method with Spock's @ConditionBlock
annotation and put it in a place where Spock can find it at compile time (e.g. base class), the assert
keyword can even be omitted.
Note that Spock's conditions aren't the same as Groovy's power asserts. If you catch Spock's ConditionNotSatisfiedError
, you'll get further information about the assertion (e.g. a full syntax tree), which you can exploit to provide better feedback. If you don't need this additional information, then catching AssertionError
is fine.