I have a controller method like such:
def search = {
def query = params.query
...
render results as JSON
}
How do I unit test this? Specifically, how do I call search to set params.query, and how do I test the results of the method render? Is there a way to mock the render method, perhaps?
Subclass grails.test.ControllerUnitTestCase for your unit tests. Grails will automatically instantiate your controller and mock out versions of render and redirect that allow you to test the results easily. Just assign the test inputs to controller.params to set up the test.
Example:
The test looks like:
Note: you can use ControllerUnitTestCase for integration tests too, if you need a full integration environment complete with database.