jmeter之BeanShell 断言

当响应报文格式为json格式时,jmeter中beanshell 断言需要结合json工具包进行操作,将获取到的响应报文转换为json格式,然后通过相关方法获取响应报文中的各个部分,并进行结果断言;下面通过一个接口来进行操作说明;

接口:

响应报文:

{"tag":"****","code":0,"msg":"成功","data":"02f33b7e80a04ddda34df5888bfe6b89"}

断言:

当code=0时,断言成功

当code=1时,断言失败,输出错误信息:断言失败,msg:交易失败;

操作步骤:

1、添加json依赖jar包

将json.jar包置于jmeter安装目录/lib目录下,json.jar包下载地址:

链接:https://pan.baidu.com/s/1aK3DAwt2uQRoheI826of3A
提取码:l90n

2、编辑脚本,添加beanshell断言,脚本如下;

import org.json.JSONObject;

//获取响应报文
String response=prev.getResponseDataAsString();
log.info("response:"+response);
//响应报文转换为json
JSONObject responseJson = new JSONObject(response);

//获取响应报文code值
String code = responseJson.getString("code");
log.info("code:"+code);

//获取响应报文msg值
String msg = responseJson.getString("msg");
log.info("msg:"+msg);

//断言,code≠0,断言失败
if(code.equals("0")){
    Failure = false;
    FailureMessage = "断言成功,msg:" + msg ;
}else{
    Failure = true;
    FailureMessage = "断言失败,msg:" + msg ;
}
原文地址:https://www.cnblogs.com/wzl0916/p/13645918.html