Grails hip tip: testing RESTful services

how-to
Jun 15, 20093 mins

Are you building hip RESTful services in Grails that leverage XML? Because it’s your bag, do you want to test these services, easily? If so, then I’ve got a tool for you, baby. Grails’ Functional Testing Plugin makes verifying XML based web services a snap, baby!

This handy dandy plug-in along with Groovy’s multi-line Strings makes testing XML GETs, POSTs, PUTs, and DELETEs a breeze with as little code as possible. To get started, you must first install the plug-in like so:

$>grails install-plugin functional-test

Next, create a functional test like so:

$>grails create-functional-test MyRESTfulTest

This command will create a new Groovy file in a test/functional directory. The newly generated Groovy class is an instance of an old style JUnit 3.x test — accordingly, from here you can implement simple test methods in Groovy that have a lot of special methods at their disposal.

For example, to verify an HTTP GET to a RESTful service you can simply write:

<pre class="prettyprint"><code>void testRESTfulGET() {
 get('/currentComnWidgRes/1200992722')
 assertStatus 200
 assertContent """<?xml version="1.0" encoding="UTF-8" ?>
   <list><currentComnWidgRes id="1200992722">
   <effectiveDate>2009-04-30 00:00:00.0 EDT</effectiveDate><nib>4348.23</nib>
   </currentComnWidgRes></list>"""
}

Note the call to the plug-in’s get method; plus, note how I can compare the resulting response (in XML too, baby) via the assertContent call that can take a multi-line String — isn’t that a snap?

POSTs are as easy too — simply leverage the plug-in’s post method and have at it, man!

<pre class="prettyprint"><code>void doRESTfulPOST(){
 post("/currentComnWidgRes/"){
  body{
    """<?xml version="1.0" encoding="UTF-8" ?>
      <currentComnWidgResid="6969"><nib>-90.0</nib>
      <effectiveDate>2009-04-30</effectiveDate>
      </currentComnWidgRes>"""
    }
 }
 assertStatus 200
 assertContent """<?xml version="1.0" encoding="UTF-8" ?>
   <currentComnAcctRes id="6969">
   <effectiveDate>2009-04-30 00:00:00.0 EDT</effectiveDate>
   <nib>-90.0</nib> </currentComnAcctRes>"""
}

Note, the post method takes a closure where you can set the body via another closure and like before, you can easily verify HTTP status codes and responses.

Running the plug-in is simple too — simply type:

$>grails functional-tests

This command is quite handy as it fires up an instance of your web application; thus, the Grails Functional Testing plug-in makes functional testing, well, easy. What’s not to like about that?

You can now follow The Disco Blog on Twitter, baby!
andrew_glover

When Andrew Glover isn't listening to “Funkytown” or “Le Freak” he enjoys speaking on the No Fluff Just Stuff Tour. He also writes articles for multiple online publications including IBM's developerWorks and O'Reilly’s ONJava and ONLamp portals. Andrew is also the co-author of Java Testing Patterns, which was published by Wiley in September 2004; Addison-Wesley’s Continuous Integration; and Manning’s Groovy in Action.

More from this author