Loading...

December 28, 2009

Groovy Goodness: Powerful Assert

In Groovy 1.7 we get a lot of information if an assertion isn't true. Before we only could see the assertion was false, but now we get a breakdown of all components used in the assertion. For each component the return value is shown, so we can easily determine what component is responsible for the false assertion.

For example we run the following code:

def list = [1, 2, 3, 4, 5]
assert 4 == list.max() - 2

And the assert output is:

assert 4 == list.max() - 2
         |  |    |     |
         |  |    5     3
         |  [1, 2, 3, 4, 5]
         false

We notice how the output shows the contents of the list, the output of list.max() and the result of list.max() - 2.