我正在尝试测试以下操作:
def search = {
//keep these values so we can rerender on the filters
def cal=Calendar.getInstance()
def startCal=Calendar.getInstance()
def endCal=Calendar.getInstance()
startCal.set(Calendar.HOUR, 0);
startCal.set(Calendar.MINUTE, 0);
startCal.set(Calendar.SECOND, 0);
endCal.set(Calendar.HOUR, 0);
endCal.set(Calendar.MINUTE, 0);
endCal.set(Calendar.SECOND, 0);
startCal.set(params.int('startDate_year')?:Calendar.getInstance().get(Calendar.YEAR) -10,params.int('startDate_month')?params.int('startDate_month')-1:Calendar.JANUARY,params.int('startDate_day')?:1)
def startDate=startCal.getTime()
endCal.set(params.int('endDate_year')?:Calendar.getInstance().get(Calendar.YEAR),params.int('endDate_month')?params.int('endDate_month')-1:cal.get(Calendar.MONTH),params.int('endDate_day')?:cal.get(Calendar.DATE))//cal.get(Calendar.MONTH) cal.get(Calendar.DATE)
def endDate=endCal.getTime()
flash.customer = params.customer
flash.siteName = params.siteName
flash.review_filter=params.review_filter
flash.message=""
flash.startDate=""
flash.endDate=""
if(params.startDate_year){
flash.startDate=startDate
}
if(params.endDate_year){
flash.endDate=endDate
}
flash.success_filter=params.success_filter
//Validation Check
if(endDate>new Date()|| startDate>new Date()){
flash.message=message(code: "date.endDate.greaterror")
render(view:'list',model:[ auditInstanceList: null,auditInstanceTotal:0 ])
return
}
if(startDate>= endDate){
flash.message=message(code:"date.endDate.aftererror")
render(view:'list',model:[ auditInstanceList: null,auditInstanceTotal:0 ])
return
}
if(!params.max) {
params.max = grailsApplication.config.AUDIT_SEARCH_MAX
}
def query
def criteria = Audit.createCriteria()
query = {
and{
//between("dateCreated",startDate,endDate)
// le("dateCreated",endDate)
if(params.customer){
ilike("customerName", params.customer + '%')
}
if(params.siteName){
ilike("siteName", params.siteName + '%')
}
max:params.max
offset:params.offset
if(startDate){
ge("dateCreated",startDate)
}
if(endDate){
le("dateCreated",endDate)
}
if(params.review_filter=='true' ||params.review_filter=='false'){
eq("reviewed", params.review_filter=='true' )
}
if(params.success_filter=='0'||params.success_filter=='1'){
eq("success",params.success_filter.toInteger())
}
}
}
def results = criteria.list(params, query)
render(view:'list', model:[ auditInstanceList: results,auditInstanceTotal:results.totalCount ])
[auditInstanceList: results, auditInstanceTotal:results.totalCount]
}
我使用了@Mock(Audit) 注释,但是当我运行测试时仍然出现空指针异常。我读到标准测试和 grails 2.1.0 存在问题。这是我的测试,它在最后一个断言上失败(它返回 null)。
def populateValidParams(params) {
assert params != null
def now = new Date();
def byesterday = new Date(now.getYear(), now.getMonth(), now.getDate()-2);
println("byesterday"+byesterday)
params["customerName"] = 'sky'
params["siteName"] = '123456'
params["userName"] = 'user'
params["controllerName"] = 'control'
params["actionName"] = 'action'
params["arguments"] = 'arguments'
params["success"] = 0
params["msg"] = 'message'
params["dateCreated"] = byesterday
params["reviewed"] = false
}
void testSearch() {
populateValidParams(params)
def audit=new Audit(params)
audit.save(flush:true,failOnError: true)
def now = new Date();
def yesterday = new Date(now.getYear(), now.getMonth(), now.getDate()-1);
//Search non existing customer
params.customer='ks'
params.siteName=''
controller.search()
assert model.auditInstanceTotal == 0
//Search case insensetive
params.customer='SK'
params.siteName=''
params.endDate_year=''
controller.search()
assert model.auditInstanceTotal == 1
}
任何想法如何解决这个问题?