Search

Dark theme | Light theme

September 18, 2009

Cool Assertion Stacktrace Output in Groovy Web Console

The Groovy Web Console application is very cool. We can use it to test and run Groovy scripts right in our web browser. Today I noticed a new feature that makes using assertions even more fun. If a assertion is false the StackTrace tab shows very detailed information about why the assertion is false. The value of each object used in the assertion and results from operators are shown.

Suppose we have the following script:

class User implements Comparable {
    String username
    int compareTo(other) { this.username <=> other.username }
    String toString() { "$username" }
}

assert 0 == (new User(username: 'mrhaki') <=> new User(username: 'hubert'))

The assertion is false and in the StackTrace tab we see why:

Assertion failed: 

assert 0 == (new User(username: 'mrhaki') <=> new User(username: 'hubert'))
         |   |                            |   |
         |   mrhaki                       1   hubert
         false

 at Script1.run(Script1.groovy:7)

Very impressive! Run this script in Groovy Web Console.