我的json回复如下:
{
"glossary": {
"title": "example glossary",
"GlossDiv": {
"title": "S",
"GlossList": {
"GlossEntry": {
"ID": "SGML",
"SortAs": "SGML",
"GlossTerm": "Standard Generalized Markup Language",
"Acronym": "SGML",
"Abbrev": "ISO 8879:1986",
"GlossDef": {
"para": "A meta-markup language, used to create markup languages such as DocBook.",
"GlossSeeAlso": ["GML", "XML"]
},
"GlossSee": "markup"
}
}
}
}
}
我必须进行休息api调用,并且响应将是带有后续模式的json
{
"glossary":{
"title":{},
"glossaryDiv" :{}
}
}
我应如何验证包含上述模式的回复?
答案 0 :(得分:1)
您可以使用JSR223 Assertion。鉴于您在"语言"中选择了groovy
。在下拉列表中,您将获得Groovy built-in capabilities to parse JSON以检查所需字段的存在。
示例解决方案类似于:
def json = new groovy.json.JsonSlurper().parse(prev.getResponseData())
if (json.glossary == null) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('glossary object was not found')
return
}
if (json.glossary.title == null) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('glossary title object was not found')
return
}
if (json.glossary.GlossDiv == null) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('glossary div object was not found')
return
}
有关在JMeter测试中使用Groovy脚本的详细信息,请参阅Groovy is the New Black指南。
或者,您可以使用JSON Path Assertion项目提供的JMeter Plugins相同,但在这种情况下,功能会少得多。