【问题标题】:Return error response if assertion fails in SoapUI如果 SoapUI 中的断言失败,则返回错误响应
【发布时间】:2016-03-07 11:36:36
【问题描述】:

如果SoapUI 中的断言失败,我想返回自定义错误消息。

我已经写了断言。即使断言失败,我总是得到 OK 响应。

我尝试了以下脚本:

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()

但这会在 assert 执行之前返回。因此,断言虽然应该失败,但还是通过了。

有什么方法可以实现吗?

【问题讨论】:

    标签: soap groovy soapui soap-client


    【解决方案1】:

    这是因为脚本仅返回一条消息,但并未使其失败。此外,此处不应使用return。由于存在return,因此您的代码中的assertion 语句永远不会到达。

    这是你需要做的:

    您可以在脚本中选择以下两个选项

    1. 使用 If - 如果条件失败,则显示错误
    2. 使用断言 - 在断言失败时显示错误消息

    下面是完整的groovy脚本,注意id属性在你提供的脚本中没有找到,所以添加以避免属性丢失错误。

    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()
    

    【讨论】:

      猜你喜欢
      • 2019-01-15
      • 1970-01-01
      • 1970-01-01
      • 2020-03-22
      • 2012-11-28
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      相关资源
      最近更新 更多