jmeter实现返回json串的断言
来源:互联网 发布:软件怎么销售 编辑:程序博客网 时间:2024/05/27 09:44
本篇主要解决的是对返回的整个json串进行处理,如果是针对json串中单个属性进行处理,可以下个json插件,相关下载地址 https://jmeter-plugins.org/wiki/JSONPathExtractor/, 废话不多说下嘛是故事背景与解决方法。
背景:通过接口返回的json串中的属性位置有可能不固定,这就导致了通过jmeter自身的字符串比较来做断言行不通
思路:先获取到接口返回的信息,然后转化成对应的json或者JSONArray对象,然后通过对象进行比较,根据比较结果设置用例的状态
第一步:编写java代码比较json对象,代码需要使用阿里的fastjson的jar包,fastjson-1.2.7.jar 此版本以上的都可以
package com.my;
import java.util.Iterator;
import java.util.Set;
import com.my.TestJson;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
public class TestJson {
//private JSONArray jsonArray1;
//private JSONArray jsonArray2;
//比较两个json对象并返回对象中值不一样的属性名称
public String compareJson(JSONObject json1, JSONObject json2){
StringBuilder sb = new StringBuilder("");
Set<String> keySet1 = json1.keySet();
Set<String> keySet2 = json2.keySet();
for (Object a: keySet1){
if(keySet2.contains(a)){
Object a1 = json1.get(a);
Object a2 = json2.get(a);
if (a1.equals(a2)){
continue;
}else{
sb.append("value is not same,key is").append(a);
break;
}
}else{
sb.append("key is not same,json2 doesn't contain ").append(a);
}
}
return sb.toString();
}
//比较两个json对象组,返回第1个数据中在第2个数组里不存在的json对象
public String compareArry(JSONArray jsonArray1, JSONArray jsonArray2){
//int a = jsonArray1.size();
for (int i=0; i<jsonArray1.size(); i++){
if(jsonArray2.contains(jsonArray1.get(i))){
continue;
}
else {
return jsonArray1.getString(i);
}
}
//System.out.println("str1 is same to str2");
return null;
}
//比较两个JSONArray对象的长度,并将长度较大的数组作为compareArry方法中的第1个入参
public String bigArray(JSONArray json1, JSONArray json2){
StringBuilder sb = new StringBuilder("");
if (json1.size() != json2.size()){
return sb.append("they length is not same").toString();
}else {
return this.compareArry(json1, json2);
}
}
//如果知道返回的数组对象中只有1个json对象的时候,就用词函数比较
public String compareArryForOne(JSONArray jsonArray1, JSONArray jsonArray2){
StringBuilder sb = new StringBuilder("");
if(jsonArray1.size() != 1 || jsonArray2.size() != 1){
return sb.append("input array size is not 1").toString();
}
return this.compareJson(jsonArray1.getJSONObject(0), jsonArray2.getJSONObject(0));
}
public static void main(String[] args){
String str1 = "[{\"carrierCode\":\"VMALL-HUAWEIDEVICE\"},{\"carrierCode2\":\"VMALL-HUAWEIDEVICE\"}]";
String str2 = "[{\"carrierCode\":\"VMALL-HUAWEIDEVICE\"},{\"carrierCode1\":\"VMALL-HUAWEIDEVICE\"}]";
String str3 ="they length is not same";
JSONArray jsonArray1 = JSON.parseArray(str1);
JSONArray jsonArray2 = JSON.parseArray(str2);
TestJson a = new TestJson();
//数组值等于1的比较
//String x = a.compareArryForOne(jsonArray1, jsonArray2);
//json对象数组的长度大于1
String x = a.bigArray(jsonArray1, jsonArray2);
System.out.println(x);
if(x == null){
System.out.println("jsonArray1 is same to jsonArray2");
}else if(x.equals(str3)){
System.out.println(str3);
}else{
System.out.println("jsonArray1 is not same to jsonAarry2, jsonArray2 doesn't contain " + x);
}
}
}
第二步:把此代码打成jar包,注意打jar包的时候把main()函数部分注释掉,然后把打好的jar包放到jmeter的lib/ext目录下
第三步:重启jmeter后,在用例下增加bean shell断言,脚本如下:
import com.my.*;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.JSONArray;
import org.apache.jmeter.samplers.SampleResult;
import org.apache.jmeter.assertions.AssertionResult;
import org.apache.jmeter.assertions.*;
try{
//预期结果放在jmeter变量resource中
String str1=vars.get("resource");
//获取上个接口的返回信息
String str2=prev.getResponseDataAsString();
//字符串转换为对应的json对象
TestJson a = new TestJson();
JSONArray jsonArray1 = JSON.parseArray(str1);
JSONArray jsonArray2 = JSON.parseArray(str2);
//调用函数比较结果,如果返回为null则用例通过,否则用例不通过
String x = a.compareArryForOne(jsonArray1, jsonArray2);
log.info("ready to show xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
log.info(x);
//AssertionResult result = new AssertionResult();
if(x == null || x.equals("")){
Failure = false;
}else{
Failure = true;
FailureMessage = x;
}
}catch(Exception ex){
log.info(ex);
}
- jmeter实现返回json串的断言
- Jmeter响应断言的使用
- JMeter-断言
- JMeter断言
- jmeter 断言
- Jmeter断言
- JMeter中返回Json数据的处理方法
- 将jmeter返回的json数据转码
- Jmeter断言-响应断言
- jmeter随笔(12)--定制json响应结果断言
- <续>jmeter随笔(12)--定制json响应结果断言
- jmeter之json数据参数化、断言设置
- jmeter断言加密的响应信息(转)
- Jmeter入门实战(二)如何使用Jmeter的BeanShell断言,把响应数据中的JSON跟数据库中的记录对比
- Jmeter断言-所有断言讲解
- 断言的实现方法
- Jmeter-断言/检查点
- jmeter之断言
- 教你怎样操作编辑PDF文件,修改PDF上的文字
- 截图,截屏
- android shape的使用
- 面试题之jsp九大内置对象
- 网络请求content-type类型
- jmeter实现返回json串的断言
- C++ 的 __LINE__和 __FUNCTION__
- Git常用命令总结
- fir.im Weekly - 2016 移动开发技术大回顾
- masonry Unable to simultaneously satisfy constraints. Probably at least one of the c
- Linux开启键盘背光
- STL中调用函数对象和普通函数差异
- android那些事之Bitmap、InputStream、Drawable、byte[]、Base64之间的转换关系 复制代码
- TestNG 使用介绍