I'm trying to write some integration tests for a controller that handles REST requests in JSON format. My controller defines() create like this:
class FooController {
...
def create() {
withFormat {
html {
[fooInstance: new Foo(params)]
}
json {
[fooInstance: new Foo(params.JSON)]
}
}
}
...
}
And then I have an integration test that looks like this:
@TestFor(FooController)
class FooControllerTests extends GroovyTestCase {
void testCreate() {
def controller = new FooController()
controller.request.contentType = "text/json"
// this line doesn't seem to actually do anything
controller.request.format = 'json'
// as of 2.x this seems to be necessary to get withFormat to respond properly
controller.response.format = 'json'
controller.request.content = '{"class" : "Foo", "value" : "12345"}'.getBytes()
def result = controller.create()
assert result
def fooIn = result.fooInstance
assert fooIn
assertEquals("12345", fooIn.value)
}
}
But fooIn is always null. If I debug the test, I can see that when FooController.create() is invoked, params is empty too. Admittedly I don't know much about how the integration tests are supposed to function internally but I expected to see data representing my Foo instance.
Any ideas?