2017-07-11 41 views
1

我有一個測試用例,其中我連接到數據庫並查詢一些數據,將結果保存到屬性中,然後向API發出請求並將保存的屬性與JSON響應進行比較。SoapUI將XML中的null與JSON響應中的null進行比較

這工作,除非結果爲空。

這是數據庫的XML結果。

enter image description here

我將結果保存在腳本中斷言

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)

在這種情況下如何檢查和比較空值?

回答

1

這是因爲,jdbc結果有empty值,json有null用於第二個電話。

因此,檢查jdbc結果是否爲任何屬性/屬性爲空,然後檢查是否相等;否則檢查是否相等。

另一種方法是 - 在第一個腳本中,如果元素的jdbc響應值爲空,則將其另存爲null

+0

那麼不應該這樣工作'context.testCase.setPropertyValue('SECONDARYPHONE',null)',應該嗎?它仍然會拋出同樣的錯誤。看起來像'jsonResponse.secondaryPhone'是空的部分,因爲'context.testCase.setPropertyValue('SECONDARYPHONE',「whatever」)'打印'斷言jsonResponse.secondaryPhone == context.testCase.getPropertyValue('SECONDARYPHONE')| | | | | | | | | | |無論| | | |' –

+0

所以我檢查,如果jsonResponse.secondaryPhone是空的它失敗,然後這通過壽'斷言jsonResponse.secondaryPhone.toString()== context.testCase.getPropertyValue('SECONDARYPHONE')。toString()' –