gpt4 book ai didi

json - 使用 JSON 输出执行集成测试时出错

转载 作者:行者123 更新时间:2023-11-28 20:47:17 31 4
gpt4 key购买 nike

下面是我在 Controller 中的 Action 。我正在尝试通过集成测试来测试此操作。这也需要我模拟 session 对象。我已经开始进行集成测试,但运气不好。

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()
//}

}

下面是我写的集成测试。这是不正确的,我不确定我应该在这里放什么。输入?

    public void testQuery () {

def myController = new TuneController()
myController.request.contentType = "text/json"

myController.tuneService = tuneService

myController.listData()

String actualJSON = myController.response.contentAsString

assertNotNull(actualJSON)




}

运行测试时出现以下错误。

无法获取空对象上的属性“请求”

java.lang.NullPointerException: 无法获取 null 对象的属性“请求”

想法??

最佳答案

为这个场景制定了测试用例。下面是代码。谢谢!

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)

}

关于json - 使用 JSON 输出执行集成测试时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4020880/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com