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?
| Related odds and ends | ||
|---|---|---|
10 comments Monday 15 Jun 2009 | Developer Testing, Groovy
10 Responses to “Grails hip tip: testing RESTful services”
Thanks u r information
Looks awesome! What if I was testing a RESTful service in Groovy, without grails? Any suggestions?
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?
[...] 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). [...]
Very Interesting and knowledgeable Post
Well Written Article. I learnt New Topic from this informative article. Thanks sharing.
Very Informative Post. Nice Sharing with Knowledegable Article.
Very Very valuable Post Man.
Nice reading i will check this logic & get back you again.
Very useful resource for Web developers. I got very valuable information. Thanks