1、响应数据如下:
{
"retCode": "000000",
"retMsg": "操作成功",
"data": [
{
"id": 3,
"versionCode": "V4.2.2",
"versionState": 3,
"creator": "XXX",
"createTime": "2022-03-30 10:35:55"
},
{
"id": 2,
"versionCode": "V4.2.1",
"versionState": 2,
"creator": "xxx",
"createTime": "2022-03-08 14:52:29"
},
{
"id": 1,
"versionCode": "V4.2.0",
"versionState": 2,
"creator": "system",
"createTime": "2022-03-03 10:41:12"
}
]
}
2、数据库执行 sql 结果设置到变量中;
3、如下进行脚本断言
import org.json.*;
String res = prev.getResponseDataAsString();//获取响应结果
log.info(res);
JSONObject jsonObject = new org.json.JSONObject(res);
JSONArray data = jsonObject.getJSONArray("data");
JSONObject jsonObject1 = data.getJSONObject(0);
String versionCode = jsonObject1.getString("versionCode");
log.info("请求获取:"+versionCode);
String datares = "${version_code_1}";//获取数据库结果
log.info("数据库获取:"+datares);
if(datares.equals(versionCode)){
AssertionResult.setFailure(false); //显示断言是否失败 true是失败,false为成功
AssertionResult.setFailureMessage("断言成功");//显示断言信息
}else {
AssertionResult.setFailure(true); //显示断言是否失败 true是失败,false为成功
AssertionResult.setFailureMessage("断言失败");//显示断言信息
}