DISQUS

James Lorenzen's Blog: Testing REST Services with Groovy

  • kitplummer · 11 months ago
    Cool James. I'm not really that mad either. ;)

    The cool thing about your approach here is that it is super simple to add in non-happy-path testing too. For example you want to ensure that a PUT to an invalid "id" returns the appropriate error - do so is quick and painless.
  • James Lorenzen · 11 months ago
    I know Kit. I just like to give you a hard time.
  • Andrej · 11 months ago
    Seems the groovyx.net....json class uses a different form of json than grails itself. The json interpreter used by the test doesn't accept the json generated by my grails controller. I get the following error:



    Unquotted string 'new Date(1200092400000)'

    net.sf.json.JSONException: Unquotted string 'new Date(1200092400000)'

    at net.sf.json.util.JSONTokener.nextValue(JSONTokener.java:445)

    at net.sf.json.JSONObject._fromJSONTokener(JSONObject.java:1128)

    at net.sf.json.JSONObject._fromString(JSONObject.java:1317)

    at net.sf.json.JSONObject.fromObject(JSONObject.java:185)

    at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:139)

    at net.sf.json.JSONSerializer.toJSON(JSONSerializer.java:103)

    at net.sf.json.groovy.JsonSlurper.parseText(JsonSlurper.java:78)

    at groovyx.net.http.ParserRegistry.parseJSON(ParserRegistry.java:185)



    Here's the json generated by the controller:



    {"id":18,"class":"Kandidaat","aanbiedingenPerPost":false,"achternaam":"Willemsen","dossierNummer":"10","etage":"3","huisletter":"a","huisnummer":"12","inschrijfDatum":new Date(1200092400000),"kamerbehoefte":"4","postcode":"1234 ab","reacties":[],"redenAfvoer":null,"soort":"Geindiceerde","straatnaam":"Nieuwe stationsstraat","voorletters":"AB","voorvoegsel":"","woningNetNummer":"10","woonplaats":"Amsterdam"}



    Any idea how to fix this?
  • jlorenzen · 1 month ago
    Looks like you have json dates set to javascript. What I think that means is that only javascript clients can consume that json. Disable that feature in your Config.groovy file and it should work.
  • James Lorenzen · 10 months ago
    See this idea plugin that formats JSON.
    http://dontmindthelanguage.wordpress.com/2009/0...
  • Wendy Xi · 3 months ago
    Thanks for the example. I would like to see how you pass params and how you get a java object.
  • spasco · 1 month ago
    Where are the groovyx libraries? They're not part of the groovy download.
  • jlorenzen · 1 month ago
    The groovyx files are a part of the HTTPBuilder library I referenced in my post.