Groovy 1.7 Power Assert

Posted on December 11, 2009. Filed under: Groovy | Tags: |

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

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


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]

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]]

(Mykola, is this what you meant?)


Make a Comment

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

6 Responses to “Groovy 1.7 Power Assert”

RSS Feed for Don’t mind the language Comments RSS Feed

Did you try it with GPathResult sequence like?

assert “Text” == root.child.element

Ofcourse I did! Check the article 😉

This is so awesome. It is way better than 1.6.x assert:

java.lang.AssertionError: Expression: (y == xml.text())

I totally agree on that! I’d say, just download RC-2, and play a little with the Groovy console!

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,, and I even link to your page here.

[…] 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). […]

Where's The Comment Form?

Liked it here?
Why not try sites on the blogroll...

%d bloggers like this: