Groovy 1.7 power asserts

posted by Ted on

Asserts were a breath of fresh air sweeping over the JVM landscape with Groovy. Now, feel the power of Groovy 1.7's power asserts!

Consider the following groovy code:

a = 9
b = 14
assert 4 == a-b
In pre-1.7 Groovy, you could still do this, but you'd get an AssertionError exception thrown. Works, but muddies the waters, so to speak. Consider the output when you run this code using 1.7 Groovy. You still get an exception stack trace, but also this:
Assertion failed: 

assert 4 == a - b
         |  | | |
         |  9 | 14
         |    -5
         false
Sweet corn! Easy to read, expressive, and powerful. Oh, I'm feeling it.

Leave a comment

blog comments powered by Disqus