[英]SOAPUI assert json response if equals to json
我在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“文件”的方法更簡潔嗎?
我將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
}
您是否需要在文檔之間進行精確匹配(除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 Schema的更多詳細信息,請參閱了解 JSON Schema。
如果您具有Ready API,則有一個內置的JSON Schema Compliance Assertion ,您可以在其中使用此架構。 如果您有soapUI,則可以調出Groovy或Java來為您執行模式驗證。 我使用json-schema-validator ,在我的測試案例中,比較歸結為:
assert JsonValidatorUtils.isJsonValid(schemaFile, jsonPayload)
所有這些似乎都需要大量工作,但是如果您將時間花在前期,則最終可能會得到可以重用的通用解決方案。
像這樣?
...
def writeResponse = slurper.parseText(messageExchange.responseContent)
writeResponse.data.eachWithIndex{dat, idx->
if(idx<35){
assert dat.idCalculation == 111
assert dat.idCalculationResult == 707
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.