As stated here:
there is no simple way to parse out json/xml responses when you´re testing a rest web service with the Grails functional testing plugin.
And it isn't really clear (to me at least) how you can post json data in the request.
Here is an example how I managed to do it. To parse the response I used
dozer:
import grails.converters.JSON
@Grab(group='net.sf.dozer', module='dozer', version='5.0')
import org.dozer.DozerBeanMapper
class MyLittlePogo{
def name
def status
}
class TempFunctionalTests extends functionaltestplugin.FunctionalTestCase {
def dozerBeanMapper = new DozerBeanMapper()
public void testPostContactSettings() throws Exception {
post("/api/rest/currentuser/contactsettings") {
headers.put("Content-Type", "application/json;charset=UTF-8")
body {
"""${new JSON(
new MyLittlePogo(
name: "aname",
status: "INSTALLED"))}"""
}
}
// Convert the response to an instance of MyLittlePogo
def responseText = new String(response.responseData_.body_)
def jsonObj = JSON.parse(responseText)
MyLittlePogo myLittlePogo = dozerBeanMapper.map( jsonObj, MyLittlePogo )
assertStatus 200
assertNotNull myLittlePogo
}
}