Groovy 中的数字格式异常

Number Format Exception in Groovy

我正在尝试将销售税率 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_TAX_RATE 并提供值 6.75.
  • 在响应中,rate 是一个字符串值。
  • 在此特定情况下,无需创建额外的 Groovy 脚本测试步骤,只需 check/compare 值,删除步骤。
  • 相反,使用上述代码为其余请求测试步骤本身添加 Script Assertion
  • 但是,有一些小的变化需要阅读回复。

这是完整的Script Assertion

//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'

您可能会有"Leave about string values. How to compare with Numbers. For example monthlyPaymentWithoutDealerAddOns is having number not string. How to deal with it?"

等问题

此处当测试用例级别自定义 属性 定义为 EXPECTED_MONTHLY_PATYMENT 且值为 782.6.

就像已经提到的那样,上面的内容可以在 Script Assertion 中阅读,如下所示

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的类型

def actualPayment = json.CustomerQuoteFinanceResponse.StandardFinanceResponse.Responses[0].StandardPaymentEngineFinanceResponse.paymentWithTaxes.monthlyPaymentWithoutDealerAddOns
log.info actualPayment.class.name //this shows the data type
def expectedPayment = context.expand('${#TestCase#EXPECTED_MONTHLY_PATYMENT}') as BigDecimal
assert actualPayment == actualPayment

鉴于此 JSON(与提供的完整内容相似,但具有结束语法):

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