Reputation: 13
I'm doing integration tests for controllers in grails.I need to pass query string in test case and I'm passing it as controller.request.queryString
for the same.
My tests is getting failed. It's giving me null value
for the query String parameter.
Controller - UserExController
Foll is the action on which i'm doing integration testing.In this I'm reading query string parameter 'uid' and getting the userInstance using GORM method
def getUser() {
//reading the query string parameter 'uid'
def userId = request.getParameter("uid")
User user = User.findByUsername(userId)
render view:'edit', model:[userInstance:user]
}
This is the test for the above written action .In this I'm passing the query string parameter and calling the action
class UserInstanceTests extends GroovyTestCase {
@Test
void testPerfSummaryApi() {
def userExController=new UserExController()
userExController.request.queryString='uid=rp123'
userExController.getUser()
def model = userExController.modelAndView.model.userInstance
assertNotNull model
}
}
I'm getting groovy.lang.MissingMethod.Exception
in the foll call:
User.findByUsername(userId) //userId is comming null
Upvotes: 1
Views: 3658
Reputation: 128
This should make things work for you (tested on Grails 2.0.4):
userExController.request.addParameter('uid', 'rp123')
However, I would recommend to turn to dmahapatro's suggestion and refactor your controller to use params.uid
instead of request.getParameter("uid")
. Or even introduce a command object. The way you are doing it now just does not seem to be the standard Grails way.
Upvotes: 0
Reputation: 50245
Change in Controller:
def userId = request.getParameter("uid")
to
def userId = params.uid
Change in int test:
userExController.request.queryString='uid=rp123'
to
userExController.params.uid='rp123'
Grails' dynamic binding helps in binding the query parameters to params
and request to request
in the controller.
Upvotes: 1