SOAPUI断言json响应(如果等于json)

时间:2018-12-19 15:04:11

标签: json groovy soapui

我在TestCase的“ CheckResult”中调用了我的服务,并且我已经设置了36个相同的部分:

{
 "data": [
    {
        "idCalculation": 111,
        "idCalculationResult": 707,
        "exchangeRate": 120.3,
        "interestRate": 4.2,
        "anuityDomesticCurrencyAmount": 165669.9171,
        "anuityForeignCurrencyAmount": 1377.1397,
        "totalDomesticCurrencyAmount": 11554978.0125,
        "totalForeignCurrencyAmount": 96051.355,
        "dti": 0.6036
    },
    {
        "idCalculation": 111,
        "idCalculationResult": 708,
        "exchangeRate": 120.3,
        "interestRate": 4.7,
        "anuityDomesticCurrencyAmount": 183875.1364,
        "anuityForeignCurrencyAmount": 1528.4716,
        "totalDomesticCurrencyAmount": 11991903.275,
        "totalForeignCurrencyAmount": 99683.3189,
        "dti": 0.5438
    },
    {
        "idCalculation": 111,
        "idCalculationResult": 709,
        "exchangeRate": 120.3,
        "interestRate": 5.2,
        "anuityDomesticCurrencyAmount": 202349.3784,
        "anuityForeignCurrencyAmount": 1682.0397,
        "totalDomesticCurrencyAmount": 12435285.0834,
        "totalForeignCurrencyAmount": 103368.9533,
        "dti": 0.4941
    },
}
 ... and 33 others
 ]
}

我想用我的控件json(也是35个对象)检查前35个对象(从断言中排除最后一个对象)。我还要检查值,属性每个对象中是否有确切的数字。

我已经开始过这样的事情:

import groovy.json.JsonSlurper
import groovy.json.JsonOutput

def slurper = new JsonSlurper()
def writeResponse = slurper.parseText(messageExchange.responseContent)

def data0 = writeResponse.data[0]
def data1 = writeResponse.data[1]
def data2 = writeResponse.data[2]
def data3 = writeResponse.data[3]

//for(i=0, i<36, i++){
//if(data[i].idCalculation == 111) return true
//if(data[i].idCalculationResult == 707) return true
//}


log.info(data0)

可以肯定,在soapui中比较两个json“文件”的方法更简洁吗?

3 个答案:

答案 0 :(得分:1)

我将JsonSlurpify这两个JSON文件。 (格式相同,对吧?)

假设结果在两个地方的排序方式相同,那么您应该能够简单地循环遍历条目。像这样:

def jsonFile1 = slurper.parseText(messageExchange.responseContent)
def jsonFile2 = slurper.parseText([whereEverYouHaveYourControlJsonFile])
for (def x=0; x<35; x++) {
    assert jsonFile1.data[x].idCalculation == jsonFile2.data[x].idCalculation
    assert jsonFile1.data[x].idCalculationResult== jsonFile2.data[x].idCalculationResult
    // etc... Repeat for each variable
}

答案 1 :(得分:1)

您是否需要在文档之间进行精确匹配(除final元素外),或者只是确保JSON负载符合特定模式?如果是后者,则可以使用JSON模式来验证有效负载。这将检查有效负载的结构(包括必需和可选元素)和值(不一定是精确值,但是值是否满足特定模式)。

例如,用于验证有效负载的最小JSON模式为:

{
    "$schema": "http://json-schema.org/draft-04/schema#",
    "title": "Stack Overflow",
    "description": "Minimal JSON schema",
    "type": "object",
    "properties": {
        "data": {
            "type": "array",
            "items": [
                {
                    "type": "object",
                    "properties": {
                        "idCalculation": {
                            "minimum": 0,
                            "maximum": 1000,
                            "type": "integer"
                        },
                        "idCalculationResult": {
                            "minimum": 0,
                            "maximum": 1000,
                            "type": "integer"
                        },
                        "exchangeRate": {
                            "type": "number",
                            "minimum": 0,
                            "pattern": "^[0-9]{3}.[0-9]{1}$"
                        },
                        "interestRate": {
                            "type": "number",
                            "minimum": 0,
                            "pattern": "^[0-9]{1,2}.[0-9]{1,2}$"
                        },
                        "anuityDomesticCurrencyAmount": {
                            "type": "number",
                            "minimum": 0
                        },
                        "anuityForeignCurrencyAmount": {
                            "type": "number",
                            "minimum": 0
                        },
                        "totalDomesticCurrencyAmount": {
                            "type": "number",
                            "minimum": 0,
                            "exclusiveMinimum": true
                        },
                        "totalForeignCurrencyAmount": {
                            "type": "number",
                            "minimum": 0
                        },
                        "dti": {
                            "type": "number",
                            "minimum": 0
                        }
                    },
                    "required": [
                        "idCalculation",
                        "idCalculationResult",
                        "exchangeRate",
                        "interestRate",
                        "anuityDomesticCurrencyAmount",
                        "anuityForeignCurrencyAmount",
                        "totalDomesticCurrencyAmount",
                        "totalForeignCurrencyAmount",
                        "dti"
                    ]
                }
            ]
        }
    },
    "required": ["data"]
}

由您来决定使用哪种模式验证有效载荷内容的精确度取决于您。有关JSON模式的更多详细信息,请参见Understanding JSON Schema

如果您拥有Ready API,则可以使用内置的JSON Schema Compliance Assertion来使用此架构。如果您有soapUI,则可以调出Groovy或Java来为您执行模式验证。我使用json-schema-validator,在我的测试案例中,比较归结为:

assert JsonValidatorUtils.isJsonValid(schemaFile, jsonPayload)

所有这些似乎都需要很多工作,但是如果您将时间花在前期,则最终可能会得到可以重复使用的通用解决方案。

答案 2 :(得分:0)

像这样吗?

...
def writeResponse = slurper.parseText(messageExchange.responseContent)
writeResponse.data.eachWithIndex{dat, idx->
    if(idx<35){
        assert dat.idCalculation == 111
        assert dat.idCalculationResult == 707
    }
}