我正在使用Jmeter来验证我的静态API的响应对象,在这里我需要验证数据以及返回对象的结构。 我正在尝试使用JsonSlurper来执行此操作,但这似乎不是一个好方法,因为它无法深度扫描响应。我可以使用其他什么方法来扫描整个响应?有帮助吗?
def parsedJson1 = new groovy.json.JsonSlurper().parseText(response1.getResponseBodyContent())
if (parsedJson1 != actual) {
AssertionResult.setFailure(true)
AssertionResult.setFailureMessage('Mismatch between expected and actual JSON')
}
}''');
Assertion error: true
Assertion failure: false
Assertion failure message: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: response1 for class: Script72
IAm收到此错误:
adult,belongs_to_collection,budget,genres,homepage,id,imdb_id,original_language,original_title,overview,popularity,poster_path,production_companies,production_countries,release_date,revenue,runtime,spoken_languages,status,tagline,title,video,vote_average,vote_count
答案 0 :(得分:0)
您可以使用以下选项:
JSONObject obj1 = "expected json"
JSONObject obj2 = "actual json"
ObjectMapper mapper = new ObjectMapper();
JsonNode expected = mapper.readTree(obj1.toString());
JsonNode actual = mapper.readTree(obj2.toString());
if (!expected.equals(actual)) {
AssertionResult.setFailure(true)
}
JsonParser parser = new JsonParser();
JsonElement expected = parser.parse("expected json");
JsonElement actual= parser.parse("actual json");
if (!expected.equals(actual) {
AssertionResult.setFailure(true)
}
JSONAssert.assertEquals("expected json", "actual json", false);
更多信息:The Easiest Way To Compare REST API Responses Using JMeter