2016-03-07 78 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()