使用JSON输出执行集成测试时出错 [英] Error while executing Integration Test with JSON output

查看:118
本文介绍了使用JSON输出执行集成测试时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我在控制器中的操作.我正在尝试通过集成测试来测试此操作.这也需要我模拟会话对象.我已经开始进行集成测试,但是运气不好.

Below is my action in the controller. I am trying to test this action via an integration test. This would require me to mock the session objects as well. I have started with the integration test, but have no luck with it.

def listData= {

    def playerId=session["playerId"]    

    tuneInstanceList = tuneService.calculateId(playerId)


    def listResult = [total: tuneInstanceList.size(), items: tuneInstanceList]

    render listResult as JSON;

}

下面是我的服务类中的CalculateId方法:

Below is the CalculateId method in my service class:

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

}

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

Below is the Integration Test that I wrote. This isn’t correct and I am not sure what I should be putting up here. Inputs?

    public void testQuery () {

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

    myController.tuneService = tuneService

    myController.listData()

    String actualJSON = myController.response.contentAsString

    assertNotNull(actualJSON)




}

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

I get the below error when I run the test.

无法在空对象上获取属性"request"

Cannot get property 'request' on null object

java.lang.NullPointerException:无法在空对象上获取属性"request"

java.lang.NullPointerException: Cannot get property 'request' on null object

想法?

推荐答案

为此场景制定了测试用例.下面是代码.谢谢!

Worked out the Test Case for this scenario.Below is the code. Thanks!

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输出执行集成测试时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆