为什么在测试我的控制器时,我得到一个无法转换对象'null'的错误?

时间:2012-04-06 06:51:21

标签: grails spock

我有一个像这样的控制器:

 def unCompletedTasks() { 
      def user = User.get(springSecurityService.principal.id) 
      def choice = params.managersProject 
      params.max = Math.min(params.max ? params.int('max') : 10,100) 
      def search = Tasks.createCriteria().list(max: params.max as Integer, offset: params.offset as Integer, order: params.order as String, sort : params.sort) { 
            and { 
                  project { 
                     like('name',"${choice}") 
                  } 
                  eq('completed',false) 
                  lt('endDate',new Date().clearTime()) 
            } 
      } 

      [tasksInstanceList : search, tasksInstanceTotal: search.getTotalCount() ] 
    } 

我想测试一下。我在Spock中写了一个测试规范:

    def 'user should be displayed unCompletedTasks' () { 

            setup: "set the required objects" 
            def tasksController = new TasksController() 
            tasksController.springSecurityService = [principal: [id:tasksInstance.id]] 
            tasksController.params.managersProject = "testing" 
           //other codings goes here 

            when: 
            def model = tasksController.unCompletedTasks() 

            then: 
            model.tasksInstanceTotal == 1 

            where: 
            //required fields 
    } 

当我跑步时,我收到这样的错误:

 user should be displayed unCompletedTasks(mnm.schedule.TasksSpec) 
|  org.codehaus.groovy.runtime.typehandling.GroovyCastException: Cannot cast object 'null' with class 'org.codehaus.groovy.runtime.NullObject' to class 'java.lang.Integer' 
        at mnm.schedule.TasksController.unCompletedTasks(TasksController.groovy:39) 
        at mnm.schedule.TasksSpec.user should be displayed unCompletedTasks(TasksSpec.groovy:59) 

我不知道哪里出错了。

提前致谢。

1 个答案:

答案 0 :(得分:0)

听起来与Problems casting a null object with Spock中讨论的问题相同。解决方案是升级到Grails 2.0.2。