如何使用Grails为以下代码编写JUnit测试用例?

时间:2017-10-16 13:04:58

标签: grails junit

如何使用Grails为以下代码编写JUnit测试用例?

使用Grails 2.4.4和JDK 1.7。

getLastUploadAlertDate是服务类中的一个方法,我正在尝试为它编写JUnit。

public Map getLastUploadAlertDate(Long patientId) 
  {

    log.debug("Enter(Method) - RS- LUP{}")
    Map result = [:]

    try {
        Sql db = new Sql(dataSource)
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
        String q = "select max(reminder_date) from reminder_history where trigger_type = 2 and user_id = ${patientId}"
        def rawDate = db.firstRow(q)
        String strDate = (rawDate.getAt(0)).toString()
        if(!strDate.equals("null") || strDate != "null") {
            Date date = sdf.parse(strDate)
            result.status = true
            result.date = date
            return result
        }
        else {
            result.status = false
            return result
            }
    }catch(Exception e) {
        log.error("Exception while getLastUploadAlertDate" + e)
        result.status = false
        return result
        }
}

据我所知,我为它编写了测试用例如下:

  void test_getLastUploadAlertDate_UT_01(){
    ReminderService reminderService = new ReminderService()
    Long patientId 
    boolean status = false
    def dataSource=mockFor(DataSource)
    reminderService.dataSource=dataSource.createMock()
    def sql=mockFor(Sql)
    sql.demand.static.firstRow{String query, LinkedHashMap params->
        return
    }
    SimpleDateFormat sdf = null
    sdf = new SimpleDateFormat("dd.MM.yyyy")
    sdf.setLenient(false)
    def rawDate=mockFor(Date)
    rawDate.demand.static.firstRow{String query, LinkedHashMap params->
        return
    }
    Map result = reminderService.getLastUploadAlertDate(patientId)
    assertEquals false, status
}

我得到了像这样的例外

  

错误| 2017-10-19 11:48:16,161 [main] ERROR hcp.ReminderService -   例外情况   getLastAdherenceAlertInDaysgroovy.lang.GroovyRuntimeException:   方法groovy.sql.Sql#的模糊方法重载。不能   解决因重叠而调用[null]的方法   原型之间:[interface java.sql.Connection] [interface   的javax.sql.DataSource]

     

错误| 2017-10-19 11:48:16,161 [主要]错误   hcp.ReminderService - 异常时   getLastUploadAlertInDaysgroovy.lang.GroovyRuntimeException:Ambiguous   方法groovy.sql.Sql#的方法重载。无法解决   由于原型重叠而调用[null]的方法   之间:[interface java.sql.Connection] [interface   的javax.sql.DataSource]

0 个答案:

没有答案