Grails Unit test case for cookies

03 / Mar / 2011 by Uday Pratap Singh 2 comments

In one of my project we were reading the cookies so that we could do the processing accordingly. Now reading the cookie is easy but writing a unit test case for that action was little bit tricky for me because I wanted to test the behavior in case of when I find the cookie and when there is no desired cookie in the request. So the question was how to add cookie into the mocked request.

To do this I did the following in my unit test setup

Cookie cookie = new Cookie("APPNAME", "ABCD")
mockRequest.cookies = [cookie].toArray()

Now I can test my action easily :).

Hope it helps
Uday Pratap Singh


comments (2)

Leave a Reply

Your email address will not be published. Required fields are marked *