Thursday, October 25, 2007

Effectively using Groovy Console in Grails App

Each chapter in The Definitive Guide to Grails continues to impress me. Currently I am on Chapter 4 which discusses The Application Domain. In this chapter the author uses the Groovy console within the grails app to test the domains to get some quick feedback. I got to say this ability is awesome! Not only could you do some quick testing, but I am sure it would be great for debugging purposes as well. Now obviously we want to keep testing in the console at a minimum and continue writing those unit tests, but that isn't until chapter 6. So until then here is how you can use the console in your grails app.

Prerequisites
1) Already created a grails app: grails create-app bookmarks
2) Created at least one domain: grails create-domain-class Bookmark
3) Added some properties to the Bookmark domain (URL url)

Now cd into the bookmarks directory and run grails console. This will compile your domain class and make it available to the console. Then execute the following (CRTL + R):

def bookmark = new Bookmark(url: new URL("http://jlorenzen.blogspot.com"))
println 'Bookmark url is ' + bookmark.url
You should see the following output:
Bookmark url is http://jlorenzen.blogspot.com

1 comments:

Joe Kueser said...

James, have you seen that Hudson has a Groovy console as well? Pretty sweet.