You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在SOAP UI中用Groovy JsonSlurper循环解析JSON子节点并断言

Is looping through each result node for individual assertions feasible?

Absolutely—this approach is not only feasible but highly recommended over your current bulk assertion method. It provides far more granular feedback when an assertion fails, making debugging much easier, and it’s more maintainable as your list of microservices grows or changes.

Here’s how you can implement it in Groovy:

// Imports
import groovy.json.JsonSlurper

// Grab and parse the response
def responseMessage = messageExchange.response.responseContent
// Clean up any wrapping quotes or escaped characters (adjust if needed based on actual response format)
def trimmedResponse = responseMessage.replaceAll('^"|"$', '').replaceAll('\\\\/', '/')
def jsonSlurper = new JsonSlurper().parseText(trimmedResponse)

// Verify top-level status first
assert jsonSlurper.status == "Healthy" : "Top-level status is not 'Healthy'"
assert jsonSlurper.results != null && !jsonSlurper.results.isEmpty() : "Results list is empty or null"

// Define expected results as a list of maps (matches the structure of your JSON)
def expectedResults = [
    [name: "Microservice one", status: "Healthy", description: "Url check MSOneURI success : status(OK)"],
    [name: "Microservice two", status: "Healthy", description: "Url check MSTwoURI success : status(OK)"],
    [name: "Microservice three", status: "Healthy", description: "Url check MSThreeURI success : status(OK)"],
    [name: "Microservice four", status: "Healthy", description: "Url check MSFourURI success : status(OK)"],
    [name: "Microservice five", status: "Healthy", description: "Url check MSFiveURI success : status(OK)"]
]

// Loop through each result and assert individual fields
jsonSlurper.results.eachWithIndex { actualResult, index ->
    def expectedResult = expectedResults[index]
    
    // Assert with descriptive messages for easier debugging
    assert actualResult.name == expectedResult.name : 
        "Name mismatch at index $index: Expected '${expectedResult.name}', got '${actualResult.name}'"
    assert actualResult.status == expectedResult.status : 
        "Status mismatch for '${actualResult.name}': Expected '${expectedResult.status}', got '${actualResult.status}'"
    assert actualResult.description == expectedResult.description : 
        "Description mismatch for '${actualResult.name}': Expected '${expectedResult.description}', got '${actualResult.description}'"
}

Key improvements over your original code:

  • Granular error messages: If an assertion fails, you’ll immediately know which microservice and which field is incorrect (e.g., "Status mismatch for 'Microservice two': Expected 'Healthy', got 'Unhealthy'").
  • Maintainability: Adding or removing microservices only requires updating the expectedResults list, rather than modifying multiple string variables.
  • Readability: The code clearly maps each actual result to its expected counterpart, making it easier to follow and modify.

This implementation will work reliably as long as the order of microservices in the JSON response matches the order in your expectedResults list. If the order might vary, you could adjust the code to match by name instead of index (e.g., using a map to look up expected values by microservice name).

内容的提问来源于stack exchange,提问作者Priyanka Ray

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 07:26:08