如何通过 groovy 订购 json 输出?
How to order a json output via groovy?
我发现我的 SOAP UI 脚本有些异常。我只想断言数据是正确的,所以我在下面写了这段代码:
import com.eviware.soapui.support.GroovyUtils
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def json = new JsonSlurper().parseText(response)
def jsonFormat = (response).toString()
def policies = [
[x: 28, xxx: 41, xxxxx: 1, name: 'Individual 18-50', aaa: true],
[x: 31, xxx: 41, xxxxx: 1, name: 'Individual 51-60', aaa: true],
[x: 34, xxx: 41, xxxxx: 1, name: 'Individual 61-75', aaa: true],
[x: 37, xxx: 41, xxxxx: 1, name: 'Individual 76-85', aaa: false]
]
log.warn json.policies
log.error policies
assert json.policies == policies
当我查看 log.warn 和 log.error 信息时,它以错误的顺序显示 json 响应,因为它首先显示 'isActive' 字段。
log.warn json.policies
显示:
[{aaa=true, xx=28, xxxxx=1, name=Individual 18-50, xxxx=41}, {aaa=true, x=31, xxxxx=1, name=Individual 51-60, xxx=41}, {aaa=true, x=34, xxxxx=1, name=Individual 61-75, xxx=41}, {aaa=true, x=37, xxxxx=1, name=Individual 76-85, xxx=41}]
log.error policies
显示:
[{x=28, xxx=41, xxxxx=1, name=Individual 18-50, aaa=true}, {x=31, xxx=41, xxxxx=1, name=Individual 51-60, aaa=true}, {x=34, xxxx=41, xxxxxx=1, name=Individual 61-75, aaa=true}, {x=37, xxx=41, xxxxx=1, name=Individual 76-85, aaa=false}]
如何使用 json.policies
以正确的顺序显示 DTO,以便它作为策略以正确的顺序显示?
还有一件不寻常的事,我运行测试用例10次,这个断言检查的测试步骤10次中有3次通过了。它不应该通过,就好像你将最后一个 DTO 与 policies
的结尾进行比较一样,它将 isActive
显示为 false,最后一个在 json.policies
中是活动的] 是 真。
你快接近了。但是,有几件事需要更正。
- 您不必转换
json
toString.
- 列表应该在比较之前排序。
- 并且每个地图条目在日志中的显示顺序无关紧要,每个地图都可以进行比较。
这是Script Assertion
//Check if the response is empty or not
assert context.response, 'Response is empty'
def json = new groovy.json.JsonSlurper().parseText(context.response)
//Assign the policies from current response; assuming above json contains that; change below statement otherwise.
def actualPolicies = json.policies
//Expected Polities
def expectedPolicies = [
[id: 28, providerId: 41, coverTypeId: 1, name: 'Individual 18-50', isActive: true],
[id: 31, providerId: 41, coverTypeId: 1, name: 'Individual 51-60', isActive: true],
[id: 34, providerId: 41, coverTypeId: 1, name: 'Individual 61-75', isActive: true],
[id: 37, providerId: 41, coverTypeId: 1, name: 'Individual 76-85', isActive: false]
]
log.info "List from response $actualPolicies"
log.info "List from policies $expectedPolicies"
//Sort the list and compare; each item is a map and using id to compare both
assert expectedPolicies.sort{it.id} == actualPolicies.sort{it.id}, 'Both policies are not matching'
您可以快速在线demo根据您的描述固定数据看效果比较。
我发现我的 SOAP UI 脚本有些异常。我只想断言数据是正确的,所以我在下面写了这段代码:
import com.eviware.soapui.support.GroovyUtils
import groovy.json.JsonOutput
import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def json = new JsonSlurper().parseText(response)
def jsonFormat = (response).toString()
def policies = [
[x: 28, xxx: 41, xxxxx: 1, name: 'Individual 18-50', aaa: true],
[x: 31, xxx: 41, xxxxx: 1, name: 'Individual 51-60', aaa: true],
[x: 34, xxx: 41, xxxxx: 1, name: 'Individual 61-75', aaa: true],
[x: 37, xxx: 41, xxxxx: 1, name: 'Individual 76-85', aaa: false]
]
log.warn json.policies
log.error policies
assert json.policies == policies
当我查看 log.warn 和 log.error 信息时,它以错误的顺序显示 json 响应,因为它首先显示 'isActive' 字段。
log.warn json.policies
显示:
[{aaa=true, xx=28, xxxxx=1, name=Individual 18-50, xxxx=41}, {aaa=true, x=31, xxxxx=1, name=Individual 51-60, xxx=41}, {aaa=true, x=34, xxxxx=1, name=Individual 61-75, xxx=41}, {aaa=true, x=37, xxxxx=1, name=Individual 76-85, xxx=41}]
log.error policies
显示:
[{x=28, xxx=41, xxxxx=1, name=Individual 18-50, aaa=true}, {x=31, xxx=41, xxxxx=1, name=Individual 51-60, aaa=true}, {x=34, xxxx=41, xxxxxx=1, name=Individual 61-75, aaa=true}, {x=37, xxx=41, xxxxx=1, name=Individual 76-85, aaa=false}]
如何使用 json.policies
以正确的顺序显示 DTO,以便它作为策略以正确的顺序显示?
还有一件不寻常的事,我运行测试用例10次,这个断言检查的测试步骤10次中有3次通过了。它不应该通过,就好像你将最后一个 DTO 与 policies
的结尾进行比较一样,它将 isActive
显示为 false,最后一个在 json.policies
中是活动的] 是 真。
你快接近了。但是,有几件事需要更正。
- 您不必转换
json
toString. - 列表应该在比较之前排序。
- 并且每个地图条目在日志中的显示顺序无关紧要,每个地图都可以进行比较。
这是Script Assertion
//Check if the response is empty or not
assert context.response, 'Response is empty'
def json = new groovy.json.JsonSlurper().parseText(context.response)
//Assign the policies from current response; assuming above json contains that; change below statement otherwise.
def actualPolicies = json.policies
//Expected Polities
def expectedPolicies = [
[id: 28, providerId: 41, coverTypeId: 1, name: 'Individual 18-50', isActive: true],
[id: 31, providerId: 41, coverTypeId: 1, name: 'Individual 51-60', isActive: true],
[id: 34, providerId: 41, coverTypeId: 1, name: 'Individual 61-75', isActive: true],
[id: 37, providerId: 41, coverTypeId: 1, name: 'Individual 76-85', isActive: false]
]
log.info "List from response $actualPolicies"
log.info "List from policies $expectedPolicies"
//Sort the list and compare; each item is a map and using id to compare both
assert expectedPolicies.sort{it.id} == actualPolicies.sort{it.id}, 'Both policies are not matching'
您可以快速在线demo根据您的描述固定数据看效果比较。