1

我正在尝试将 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"
                        }
                    }
                }
            }
        }]
    }
}
}
4

2 回答 2

3

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

  • 定义一个测试用例级别的自定义属性EXPECTED_TAX_RATE,并提供值作为6.75.
  • 在响应中,rate是一个字符串值。
  • 在这种特殊情况下,无需创建额外的 Groovy 脚本测试步骤来检查/比较值,删除步骤。
  • 相反,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'

您可能有诸如“离开关于字符串值。如何与数字进行比较。例如monthlyPaymentWithoutDealerAddOns有数字而不是字符串。如何处理?”之类的问题。

在这里,当一个测试用例级别的自定义属性被定义为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
于 2018-01-24T00:33:32.937 回答
0

鉴于此 JSON(类似于提供的完整 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
于 2018-01-24T00:47:06.947 回答