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
share|improve this question

2 Answers

up vote 0 down vote accepted

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.

share|improve this answer
Thanks for the reply but changing userExController.request.queryString='uid=rp123' to userExController.params.uid='rp123' does not change anything. error persists. Why make changes to the controller as the controller code is working fine. I dont think there is need to change already running code to make integration tests work. – Riya Pathak 2 days ago

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.