在SoapUI中,可以使用Groovy脚本断言来验证JSON响应中是否存在某些值。下面是一个示例脚本,假设我们正在测试一个名为“response”的JSON响应,并将其与一个名为“expectedValues”的字符串数组进行比较:
import groovy.json.JsonSlurper
// Parse the JSON response
def responseJson = new JsonSlurper().parseText(context.expand('${response#response}'))
// Define the expected values
def expectedValues = ['value1', 'value2', 'value3']
// Loop through each expected value to verify if it is present in the JSON response
expectedValues.each { value ->
// Use the 'containsKey' method to check if the key exists in the response
assert responseJson.containsKey(value): "The response does not contain ${value}"
}
在上面的示例中,我们首先将JSON响应解析为Groovy对象,然后使用包含所需值的字符串数组来验证响应中的每个值是否存在。如果任何值不存在于响应中,则脚本将抛出一个断言失败异常。