使用JMeter进行REST服务测试:我有确切的JSON是我的预期结果,请帮我在JMeter中定义这个断言。例如,我对GET请求的预期响应恰好是:
[ {
"id" : "FR",
"description" : "France",
"defaultLanguageId" : "fr",
"defaultTimezone" : "Europe/Paris"
}, {
"id" : "IT",
"description" : "Italy",
"defaultLanguageId" : "it",
"defaultTimezone" : "Europe/Rome"
} ]
答案 0 :(得分:1)
我会选择JSR223 Assertion和Groovy语言:
将以下代码放入JSR223 Assertion" Script"面积:
import groovy.json.JsonSlurper
JsonSlurper slurper = new JsonSlurper();
def expected = slurper.parseText("[\n" +
" {\n" +
" \"id\": \"FR\",\n" +
" \"description\": \"France\",\n" +
" \"defaultLanguageId\": \"fr\",\n" +
" \"defaultTimezone\": \"Europe/Paris\"\n" +
" },\n" +
" {\n" +
" \"id\": \"IT\",\n" +
" \"description\": \"Italy\",\n" +
" \"defaultLanguageId\": \"it\",\n" +
" \"defaultTimezone\": \"Europe/Rome\"\n" +
" }\n" +
"]");
def actual = slurper.parseText(prev.getResponseDataAsString());
if (expected != actual) {
SampleResult.setSuccessful(false);
}
使用这种方法,您将能够比较JSON结构,并且在不同节点顺序或额外空间或其他情况下它不会失败。如果您还需要检查订单和标记 - 只需在Equals
模式下使用普通Response Assertion,例如:
参考文献: