本文介绍了为什么在测试我的控制器时,我无法投射对象'空'错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
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()。列表(最大值:params.max为整数,偏移量:params.offset为整数,顺序:params.order为String,排序:params.sort){
和{
项目{'b $ b like('name',$ {choice})
}
eq('completed',false)
lt('endDate',new Date()。 clearTime())
}
}
[tasksInstanceList:search,tasksInstanceTotal:search.getTotalCount()]
}
我想测试这个。我在Spock中写了一个测试规范,如下所示:
def'user should be displayed unCompletedTasks'(){
setup:设置所需的对象
def tasksController = new TasksController()
tasksController.springSecurityService = [principal:[id:tasksInstance.id]]
tasksController.params.managersProject =testing
//其他编码转到这里
当:
def model = tasksController.unCompletedTasks()
then:
model.tasksInstanceTotal == 1
其中:
//必填字段
}
当我运行时,出现如下错误:
用户应显示unCompletedTasks(mnm .schedule.TasksSpec)
| org.codehaus.groovy.runtime.typehandling.GroovyCastException:无法使用类'org.codehaus.groovy.runtime.NullObject'将对象'null'强制转换为类java.lang.Integer'
at mnm.schedule.TasksController .unCompletedTasks(TasksController.groovy:39)应显示mnm.schedule.TasksSpec.user中的
unCompletedTasks(TasksSpec.groovy:59)
我不知道我错在哪里。
预先感谢。 问题 - 铸造一个null对象与Spock>用Spock投射一个空对象的问题。解决方案是升级到Grails 2.0.2。
I have a controller like this :
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() ]
}
I want to test this. I wrote a test specification in Spock like this:
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
}
When I run, I get a error like this :
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)
I don't know where I went wrong.
Thanks in advance.
解决方案
Sounds like the same problem as discussed in Problems casting a null object with Spock. The solution is to upgrade to Grails 2.0.2.
这篇关于为什么在测试我的控制器时,我无法投射对象'空'错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!