使用JSON输出执行集成测试时出错
下面是我在控制器中的操作。我试图通过集成测试来测试此操作。这也需要我模拟会话对象。我已经开始了集成测试,但是没有成功使用JSON输出执行集成测试时出错,json,testing,integration,Json,Testing,Integration,下面是我在控制器中的操作。我试图通过集成测试来测试此操作。这也需要我模拟会话对象。我已经开始了集成测试,但是没有成功 def listData= { def playerId=session["playerId”] tuneInstanceList = tuneService.calculateId(playerId) def listResult = [total: tuneInstanceList.size(), items: tuneInstance
def listData= {
def playerId=session["playerId”]
tuneInstanceList = tuneService.calculateId(playerId)
def listResult = [total: tuneInstanceList.size(), items: tuneInstanceList]
render listResult as JSON;
}
下面是我的服务类中的CalculateId方法:
List<Tune> calculateId(String playerId) {
try{
//read the sql file
String playerSql = grailsApplication.mainContext.getResource('classpath:' + Constants.PLAYER_FILE).inputStream.text
def sql = new groovy.sql.Sql(dataSource)
def params = [playerId:playerId]
def tuneInstanceList = new ArrayList<Tune>()
def results = sql.rows(playerSql, params)
tuneInstanceList = results.each {
def tune = new Tune()
tune.setPlayerId it.player_id
tuneInstanceList.add tune
}
return tuneInstanceList
}catch (Exception ex) {
log.error ex.message, ex
throw ex
}
//finally {
//sql.close()
//}
当我运行测试时,我得到以下错误
无法获取null对象上的属性“request”
java.lang.NullPointerException:无法获取null对象上的属性“request”
想法???为这个场景设计了测试用例。下面是代码。谢谢
public void testJSONQuery () {
def tuneController = new TuneController()
tuneController.request.contentType = "text/csv"
tuneController.tuneService = tuneService
tuneController.session["playerId"]='AF67H'
tuneController.listData()
String tuneJSON = tuneController.response.contentAsString
log.info ('Number of Records on execution of query is' + tuneJSON.substring(9,10))
//Checks if the record count is greater than zero
assertTrue (new Integer(tuneJSON.substring(9,10)).intValue() > 0)
}
制定了此场景的测试用例。下面是代码。谢谢
public void testJSONQuery () {
def tuneController = new TuneController()
tuneController.request.contentType = "text/csv"
tuneController.tuneService = tuneService
tuneController.session["playerId"]='AF67H'
tuneController.listData()
String tuneJSON = tuneController.response.contentAsString
log.info ('Number of Records on execution of query is' + tuneJSON.substring(9,10))
//Checks if the record count is greater than zero
assertTrue (new Integer(tuneJSON.substring(9,10)).intValue() > 0)
}