0

I have the following controller code:

def save(MyModel model) {
    model.save()
}

And I'm testing it using:

//e.g. 2ff59e55-ee3d-4f66-8bfa-00f355f52c49
def uuid = UUID.randomUUID.toString()
controller.request.contentType = JSON_CONTENT_TYPE
controller.request.method = 'POST'
controller.request.json = "{'uuid': '$uuid', 'description': 'test object', 'count': 1}"
controller.save()

However, every time I run the test I get,

org.apache.commons.lang.UnhandledException:
org.codehaus.groovy.grails.web.converters.exceptions.ConverterException:
org.codehaus.groovy.grails.web.json.JSONException: Value out of sequence: expected mode to be
OBJECT or ARRAY when writing '{'uuid': '2ff59e55-ee3d-4f66-8bfa-00f355f52c49', 'description': 'test object', 'count': 1}' but was INIT
2
  • String uuid = UUID.randomUUID().toString() and /{'uuid': "$uuid", 'description': 'test object'}/. Use slashy String in order to use " for GString. Commented Jun 4, 2014 at 15:19
  • Oops. I updated the question to correct the uuid part; it was just a typo. I tested again with the GString delimited by /.../ but I'm still getting the error. Commented Jun 4, 2014 at 15:26

2 Answers 2

1

The JSON converter chokes on Groovy Strings. I've solved this by slapping a .toString() on the end: "{'uuid':'$uuid'}".toString().

Sign up to request clarification or add additional context in comments.

Comments

0

Try this

void "Test the save action correctly persists an instance"() {
    when:
    Integer originalCount = MyModel.count()
    String uuid = UUID.randomUUID().toString()
    controller.request.contentType = 'application/json'
    controller.request.method = 'POST'
    controller.request.json = ['uuid': uuid, 'description': 'test object'] as JSON
    controller.save()

    then:
    assert originalCount + 1 == MyModel.count()
}

6 Comments

This worked using grails.converters.JSON. There was another JSON type option but I did not try it anymore because the one I mentioned worked on my first try. I'm familiar with the as keyword, but I'm curious why the GString wouldn't work and this one would...
Ok, so while it does not generate the error, it's not setting the properties either. Actually it looks like JSON binding isn't working at all, even just using {'description': 'test object'} does not set the description property.
I have updated my answer. It is passing. Assert statement pass that means we have one more entry in MyModel.
And for your first comment question, I don't have reason for that but "{'uuid':" + uuid + ", 'description': 'test object'}" is also working..,.
The updated test code looks pretty similar to what I have, and it's passing (i.e. the count gets updated). However, the model in the controller does not have any data in it. For example, model.description is null. What version of Grails are you testing this with?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.