Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Json Groovy中的数字格式异常_Json_Rest_Groovy_Soapui_Jsonslurper - Fatal编程技术网

Json Groovy中的数字格式异常

Json Groovy中的数字格式异常,json,rest,groovy,soapui,jsonslurper,Json,Rest,Groovy,Soapui,Jsonslurper,我试图将销售税税率6.75与我的预期值6.75(字符串格式)进行比较。我编写了下面几行Groovy代码来实现这一点,但我遇到了数字格式异常,我无法找出问题所在 Groovy代码 def jsonSlurper = new JsonSlurper() def parsedResponseJson=jsonSlurper.parseText(context.expand('${StandardFinance#Response}')) def actualSalesTaxRate = parsedRe

我试图将销售税税率6.75与我的预期值6.75(字符串格式)进行比较。我编写了下面几行Groovy代码来实现这一点,但我遇到了数字格式异常,我无法找出问题所在

Groovy代码

def jsonSlurper = new JsonSlurper()
def parsedResponseJson=jsonSlurper.parseText(context.expand('${StandardFinance#Response}'))
def actualSalesTaxRate = parsedResponseJson.CustomerQuoteFinanceResponse.StandardFinanceResponse.Responses[0].StandardPaymentEngineFinanceResponse.paymentWithTaxes.financeItemizedTaxes.salesTax.taxParameters.rate
def actualSalesTaxRate = parsedResponseJson.CustomerQuoteFinanceResponse.StandardFinanceResponse.Responses[0].StandardPaymentEngineFinanceResponse.paymentWithTaxes.financeItemizedTaxes.salesTax.taxParameters.rate
log.info actualSalesTaxRate.size()
actualSalesTaxRate = Float.parseFloat(actualSalesTaxRate)
def expectedSalesTaxRate = "6.75"
log.info expectedSalesTaxRate.size()
expectedSalesTaxRate = Float.parseFloat(expectedSalesTaxRate)
assert expectedSalesTaxRate.toString() == actualSalesTaxRate.toString(),"FAIL --- Sales Tax Rate is different"
JSON响应

{
"CustomerQuoteFinanceResponse": {
    "StandardFinanceResponse": {
        "Responses": [{
            "StandardPaymentEngineFinanceResponse": {
                "class": ".APRNonCashCustomerQuote",
                "RequestID": "1",
                "term": "48",
                "financeSourceId": "F000CE",
                "paymentWithTaxes": {
                    "class": ".FinancePaymentWithTaxes",
                    "amountFinanced": "34523.48",
                    "monthlyPayment": "782.60",
                    "monthlyPaymentWithoutDealerAddOns": 782.6,
                    "financeItemizedTaxes": {
                        "salesTax": {
                            "taxParameters": {
                                "rate": "6.75"
                            },
                            "salesTaxAmount": "2322.61"
                        }
                    }
                }
            }
        }]
    }
}
}

您不必将其转换为数字,因为响应中的值是字符串

  • 定义一个测试用例级自定义属性,比如
    EXPECTED\u TAX\u RATE
    ,并将值提供为
    6.75
  • 在响应中,
    速率
    是一个字符串值
  • 在这种特殊情况下,不需要创建额外的Groovy脚本测试步骤,只需检查/比较值,即remove步骤
  • 相反,使用上述代码为rest请求测试步骤本身添加
    脚本断言
  • 但是,需要读取响应时会有一些小的变化
下面是完整的
脚本断言

//Check the response is received
assert context.response, 'Response is empty or null'

//Read test case property for expected value as string; this way there is no need to edit the assertion; just change the property value
def expectedTaxRate = context.expand('${#TestCase#EXPECTED_TAX_RATE}')

def json = new groovy.json.JsonSlurper().parseText(context.response)

def actualTaxRate = json.CustomerQuoteFinanceResponse.StandardFinanceResponse.Responses[0].StandardPaymentEngineFinanceResponse.paymentWithTaxes.financeItemizedTaxes.salesTax.taxParameters.rate
log.info "Actual tax rate $actualSalesTaxRate"

//Now compare expected and actual
assert actualTaxRate == expectedTaxRate, 'Both tax rates are not matching'
您可能会遇到诸如“关于字符串值的问题。如何与数字进行比较。例如,
monthlyPaymentWithoutDealerAddOns
是否有数字而不是字符串。如何处理?”

这里,当一个测试用例级自定义属性被定义为
EXPECTED\u MONTHLY\u PATYMENT
,值被定义为
782.6

如上所述,可以在
脚本断言中阅读上述内容,如下所示

def expectedMonthlyPayment = context.expand('${#TestCase#EXPECTED_MONTHLY_PATYMENT}') //but this is string
您可以将实际值读取为:

def actualPayment = json.CustomerQuoteFinanceResponse.StandardFinanceResponse.Responses[0].StandardPaymentEngineFinanceResponse.paymentWithTaxes.monthlyPaymentWithoutDealerAddOns
log.info actualPayment.class.name //this shows the data type
现在需要将expectedPayment转换为actualPayment的类型

给定此JSON(与提供的完整JSON类似,但具有结束语法):

考虑以下代码:

def jsonSlurper = new groovy.json.JsonSlurper()
def json = jsonSlurper.parseText(s)
def response = json.CustomerQuoteFinanceResponse
                   .StandardFinanceResponse
                   .Responses[0]

assert 6.75 == response.StandardPaymentEngineFinanceResponse
                       .paymentWithTaxes
                       .financeItemizedTaxes
                       .salesTax
                       .taxParameters
                       .rate as Float

或者,
actualPayment
也可以转换为字符串并进行比较。Srinivasan,您有机会尝试解决方案吗?嗨,Rao,很抱歉,我忙于手动项目,无法按时尝试您的解决方案。在我的代码逻辑中有一个错误,我试图将空字符串转换为十进制,这导致了数字格式异常。非常感谢你的帮助。
def s = '''
{"CustomerQuoteFinanceResponse": {"StandardFinanceResponse": {
   "Responses": [   {   
      "StandardPaymentEngineFinanceResponse":       {   
         "class": ".APRNonCashCustomerQuote",
         "RequestID": "1",
         "term": "48",
         "financeSourceId": "F000CE",
         "paymentWithTaxes":          {   
            "class": ".FinancePaymentWithTaxes",
            "amountFinanced": "34523.48",
            "monthlyPayment": "782.60",
            "monthlyPaymentWithoutDealerAddOns": 782.6,
            "financeItemizedTaxes":             {   
               "salesTax":                {   
                  "taxParameters": {"rate": "6.75"},
                      "salesTaxAmount": "2322.61"
}}}}}]}}}
'''
def jsonSlurper = new groovy.json.JsonSlurper()
def json = jsonSlurper.parseText(s)
def response = json.CustomerQuoteFinanceResponse
                   .StandardFinanceResponse
                   .Responses[0]

assert 6.75 == response.StandardPaymentEngineFinanceResponse
                       .paymentWithTaxes
                       .financeItemizedTaxes
                       .salesTax
                       .taxParameters
                       .rate as Float