Grails

JSON Render broken for mockController

Details

  • Type: Bug Bug
  • Status: Open Open
  • Priority: Major Major
  • Resolution: Unresolved
  • Affects Version/s: 1.3.6
  • Fix Version/s: None
  • Component/s: Testing
  • Labels:
    None

Description

If you are testing a controller and extending from ControllerUnitTestCase (or mockController), JSON rendering won't work if you controller uses the generic render method:

render(contentType: "text/json") {
  success(true)
  results {
    result.each {
    def period = it.getPeriod() 
    b(id: it.id,name: it.name)
    }
  }
}

The reason for the issue can be found in MockUtils:

clazz.metaClass.render = {Map map, Closure c ->
  renderArgs.putAll(map)

  switch(map["contentType"]) {
    case null:
    break

    case "application/xml":
    case "text/xml":
      def b = new StreamingMarkupBuilder()
      if (map["encoding"]) b.encoding = map["encoding"]

      def writable = b.bind(c)
      delegate.response.outputStream << writable
      break

    default:
      println "Nothing"
      break
  }
}

Any test using a mockController with that type of render will have 'Nothing' printed to the screen. (That message should probably be improved as well)

I was able to fix it by adding the following case:

case "text/json":
  new JSonBuilder(delegate.response).json(c)
  break

Unfortunately, I'm still stuck using the older JSonBuilder, but the general fix is the same either way. I'm not sure if the config option to turn the legacy builder on or off can be checked in a unit test or not.

Activity

There are no comments yet on this issue.

People

Vote (5)
Watch (5)

Dates

  • Created:
    Updated:
    Last Reviewed: