Groovy 1.7 Power Assert
I already mentioned this in my previous post, but I wanted to go a little bit deeper on this: the new Groovy Power Assert (and no, let’s not call is GPA).
Groovy Power Assert makes assertions quite more powerful. The best way to demonstrate is ofcourse by example. When running the following code in Groovy 1.6:
a = 10 b = 9 assert 91 == a * b
This will be your output:
Exception thrown: Expression: (92 == (a * b)). Values: a = 10, b = 9 java.lang.AssertionError: Expression: (92 == (a * b)). Values: a = 10, b = 9 at ConsoleScript2.run(ConsoleScript2:4)
While quite helpful, Groovy 1.7 introduced an even better assert, which was initially developed for the Spock framework. The new output of running the above in a Groovy 1.7 console is this:
Exception thrown Assertion failed: assert 91 == a * b | | | | | 10| 9 | 90 false at ConsoleScript2.run(ConsoleScript2:4)
Which gives you much more insight in why the assert fails. You don’t have to use numbers, you can use any type of object to assert on, and the assert statement will call the toString method of that class, as demonstrated below:
def names = [ 'erik', 'marcel', 'sebastien' ] def reverse = names.reverse() assert ['sebastien', 'erik', 'marcel'] == reverse
Which will output the following:
assert ['sebastien', 'erik', 'marcel'] == reverse | | | [sebastien, marcel, erik] false
Quite funky he? Groovy now gives you some nicely formatted assert information which will help you to tackle your failed tests even faster!
PS: (okay, okay, I added this later….) The same works for special Groovy objects, like GPathResults. For example, check the following code:
def xml = new XmlParser().parseText("<test>x</test>") assert "y" == xml.text()
The result of this:
assert "y" == xml.text() | | | | | x | test[attributes={}; value=[x]] false
(Mykola, is this what you meant?)
Did you try it with GPathResult sequence like?
assert “Text” == root.child.element
Mykola Golubyev
December 12, 2009
Ofcourse I did! Check the article 😉
Erik Pragt
December 12, 2009
This is so awesome. It is way better than 1.6.x assert:
java.lang.AssertionError: Expression: (y == xml.text())
Mykola Golubyev
December 12, 2009
I totally agree on that! I’d say, just download RC-2, and play a little with the Groovy console!
Erik Pragt
December 12, 2009
Nice showcase of Groovy Power Asserts, we use them heavily at work on Groovy and Grails projects in lieu of the typical JUnit Assert methods to great effect.
In fact, I am so impressed with this feature that I was inspired to create a library for my hobby language, F#, which achieves like benefits: it’s called Unquote, http://code.google.com/p/unquote/, and I even link to your page here.
Stephen Swensen
April 3, 2011
[…] While trying to find a solution to this problem, I discovered JUnit 4's conditional assumption API. Since I had never seen it before, I wanted to talk about my usage of it here. In order to make these tests read cleanly, I am using Junit 4, Hamcrest, and HttpClient. My test itself is also written in Groovy, but it would work just as well in Java (I'm only using Groovy for spaces in test names and Power Asserts). […]
Conditionally Running Tests With JUnit 4 » Absolutely No Machete Juggling
August 19, 2011