2016-03-07 43 views
3

如果斷言在SoapUI中失敗,我想返回自定義錯誤消息。如果在SoapUI中斷言失敗,返回錯誤響應

我寫了斷言。即使斷言失敗,我總是會得到正確的迴應。

我想下面的腳本:

def assertionList = [] 

def idNotNull = (id!=null) ? "(id is not null" : assertionList.add("(id is null") 

if(!assertionList.isEmpty()) 
{ 
    return "exceptionResponse" 
} 
assert assertionList.isEmpty() : assertionList.toString() 

但在執行斷言之前,這將返回。因此斷言雖然會失敗,但通過。

有沒有一種方法可以實現這個目標?

回答

1

這是因爲該腳本僅返回一條消息,但未使其失敗。此外,此處不應使用return。由於存在return,因此您的代碼中的assertion聲明從未達到過。

以下是你需要做的:

有可以選擇任意腳本

  1. 使用下面給出如果兩個選項 - 如果條件不滿足,則顯示錯誤
  2. 使用斷言 - 顯示斷言失敗時的錯誤信息

下面是完整的groovy腳本,請注意,id屬性未找到t他提供的腳本,所以添加到避免財產缺失的錯誤。

def assertionList = [] 
def id 
def idNotNull = (id!=null) ? "(id is not null" : assertionList.add("(id is null") 
/** 
* You may use one of the below two options 
*/ 
//Option 1 : Using If condition fails, then Error 
//not required to use isEmpty() like you did or null, by default it will check 
if(assertionList){ 
    throw new Error(assertionList.toString())  
} 
//Option 2:Using assertion 
assert 0 == assertionList.size() : assertionList.toString()