开发者

Grails Integration Test RESTful Services - params Have No Content

开发者 https://www.devze.com 2023-01-23 23:43 出处:网络
Grails 1.3.5 The problem is that I\'m trying to test a RESTful web service that consumes json.I\'ve read the docs on grails.org regarding this and tested using their Book example.I can\'t get params

Grails 1.3.5

The problem is that I'm trying to test a RESTful web service that consumes json. I've read the docs on grails.org regarding this and tested using their Book example. I can't get params in the controller to contain any data. So I started googling and I ran across this:

http://maricel-tech.blogspot.com/2009/09/grails-how-to-execute-parameters-data.html

But that didn't even fix my problem. Anyone know if there is something else I need to do? I'd show my code but it is identical to the grails.org docs integration test for Book consuming json. Outside of an integration test, I can use the Firefox plugin, Post, to post json content to the web service and it works as expected. Only in the开发者_如何学Python integration test does it fail. Thanks.


I got this to work by extending GrailsUnitTestCase rather than ControllerUnitTestCase. Can someone explain to me why this is the case?


this is what I have done in my ControllerUnitTestCases

// set param 'queryString', using a map
controller.metaClass.getParams = {-> [queryString: "oil"] }

// I have read that this should work also
controller.request.params = [queryString: "oil"]


In my case I am extending GroovyTestCase, I am not using GrailsUnitTestCase neither ControllerUnitTest since I am executing an integration test, not a unit test.

Something important is to specify the content-type in your controller, since the content type is what it determines what binder to launch, just do:

controller.request.contentType = "text/json"

Also make sure you have parseRequest=true in your URLMappings.groovy for the action you are trying to test. (I think you already do since this works for you out of the integration test).

0

精彩评论

暂无评论...
验证码 换一张
取 消