我有一個測試用例,其中我連接到數據庫並查詢一些數據,將結果保存到屬性中,然後向API發出請求並將保存的屬性與JSON響應進行比較。SoapUI將XML中的null與JSON響應中的null進行比較
這工作,除非結果爲空。
這是數據庫的XML結果。
我將結果保存在腳本中斷言
import com.eviware.soapui.support.XmlHolder
def holder = new XmlHolder(messageExchange.responseContent)
context.testCase.setPropertyValue('SECONDARYPHONE', holder.getNodeValue('//*:SECONDARYPHONE'))
context.testCase.setPropertyValue('FAX', holder.getNodeValue('//*:FAX'))
然後在JSON請求API我得到
{
"portalId": 87776,
"name": "iOS Robotics",
"address1": "Update your company address",
"address2": "under Settings > My Company",
"city": "Reston",
"state": "VA",
"zip": "20191",
"primaryPhone": "unknown",
"secondaryPhone": null,
"fax": null
}
一個在斷言一步
import net.sf.json.groovy.JsonSlurper
def jsonResponse = new JsonSlurper().parseText(messageExchange.responseContent)
log.info('Second')
log.info(context.testCase.getPropertyValue('SECONDARYPHONE'))
log.info('json')
log.info(jsonResponse.secondaryPhone)
assert jsonResponse.secondaryPhone == context.testCase.getPropertyValue('SECONDARYPHONE')
我得到
斷言jsonResponse.secondaryPhone == context.testCase.getPropertyValue('SECONDARYPHONE')| | | | | | | | | | | null | | | | [email protected] | | | [ThreadIndex:0,RunCount:0,ExecutionID:5cf927e7-817f-4785-9152-f35e634cfe58] | | false | [email protected](的toString()投擲net.sf.json.JSONException)[email protected](的toString()投擲net.sf.json.JSONException)
在這種情況下如何檢查和比較空值?
那麼不應該這樣工作'context.testCase.setPropertyValue('SECONDARYPHONE',null)',應該嗎?它仍然會拋出同樣的錯誤。看起來像'jsonResponse.secondaryPhone'是空的部分,因爲'context.testCase.setPropertyValue('SECONDARYPHONE',「whatever」)'打印'斷言jsonResponse.secondaryPhone == context.testCase.getPropertyValue('SECONDARYPHONE')| | | | | | | | | | |無論| | | |' –
所以我檢查,如果jsonResponse.secondaryPhone是空的它失敗,然後這通過壽'斷言jsonResponse.secondaryPhone.toString()== context.testCase.getPropertyValue('SECONDARYPHONE')。toString()' –