Grails hip tip: testing RESTful services

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:

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!

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?

Post to Twitter

Related odds and ends
 

10 Responses to “Grails hip tip: testing RESTful services”

  1. on 16 Jul 2009 at 10:35 am web designer

    Thanks u r information

  2. on 16 Jul 2009 at 7:18 pm Eric Wendelin

    Looks awesome! What if I was testing a RESTful service in Groovy, without grails? Any suggestions?

  3. on 27 Jul 2009 at 1:04 pm Andy

    Eric- check out HTTPBuilder — while it isn’t a testing framework, you can easily manipulate RESTful actions and validate them with a test framework. Might I suggest easyb? :)


  4. [...] If you’re Hudson server is running as a system process (for example, as a Windows service as I’ve recently experienced) you might have some challenges running Grails plug-ins. Specially, if you’re server sits behind a proxy, the Hudson’s Grails service won’t leverage your id’s preferences found in your home directory (your home directory in Windows is found in the C:Documents and Settings directory and is your user name). Accordingly, you’ll see that Hudson will create a .grails directory most likely on your C: drive. Simply copy the scripts directory from your home’s .grails directory into this newly created one and things will work just fine (i.e. Grails inside of Hudson will pick up your proxy settings so as to download required plug-ins such as the ever-so-hip Functional Test plug-in). [...]

  5. on 30 Sep 2009 at 1:32 pm web design company

    Very Interesting and knowledgeable Post

  6. on 09 Oct 2009 at 8:06 am Web Development

    Well Written Article. I learnt New Topic from this informative article. Thanks sharing.

  7. on 13 Oct 2009 at 6:47 am Website Designing India

    Very Informative Post. Nice Sharing with Knowledegable Article.

  8. on 09 Dec 2009 at 6:58 am Company Incorporation India

    Very Very valuable Post Man.

  9. on 28 Dec 2009 at 11:40 am Interior Designing Company

    Nice reading i will check this logic & get back you again.

  10. on 29 Dec 2009 at 11:34 am Diamond Grinding Tools

    Very useful resource for Web developers. I got very valuable information. Thanks