码迷,mamicode.com
首页 > 系统相关 > 详细

jmeter beanshell Typed variable declaration : Object constructor错误

时间:2019-02-13 22:46:22      阅读:600      评论:0      收藏:0      [点我收藏+]

标签:数据   view   错误   iterator   new   运行   bsp   pen   ati   

从数据库取值和响应值做比较,使用beanshell如下:

技术图片
import org.json.JSONArray;
import org.json.JSONObject;
 
 
res_str = prev.getResponseDataAsString();
JSONObject res_json_obj = new JSONObject(res_str);
JSONArray dbd_json_array = new JSONArray("${dbd}"); // 数据库转json
JSONObject dbd_json_obj = new JSONObject();
FailureMessage = "";
Failure = true;
 
res_json_obj = new JSONObject(res_json_obj.get("data").toString());
if (!res_json_obj.isNull("list")) {
    JSONArray res_json_array = new JSONArray(res_json_obj.get("list").toString());
    log.info("响应长度:" + res_json_array.length());
    log.info("数据返回长度:" + dbd_json_array.length());
 
    if (res_json_array.length() == dbd_json_array.length()) {
        for (int i = 0; i < dbd_json_array.length(); i++) {
            Message = "\n第"+i+"条记录:";
            res_json_obj = res_json_array.getJSONObject(i);
            dbd_json_obj = dbd_json_array.getJSONObject(i);
 
            System.out.println(res_json_obj);
 
            Iterator keys = dbd_json_obj.keys();
            while (keys.hasNext()) {
                String key = (String) keys.next();
                String resvalue="";
                String dbdvalue="";
 
                System.out.println("key:" + key);
                if (key.equals(null) || key.equals("")) {
                    Message += key + "不存在;";
                    FailureMessage += Message;
                    Failure = true;
                    log.info(Message);
                    break;
                    }
                    
                        
                        if(res_json_obj.has(key) && dbd_json_obj.has(key)) {
                            resvalue = res_json_obj.getString(key);
                            dbdvalue = dbd_json_obj.getString(key);
                        }else {
                            Message += key + "不存在;";
                            FailureMessage += Message;
                            Failure = true;
                            log.info(Message);
                            break;
                        }
 
                        
 
                        if (resvalue.equals(dbdvalue)) {
                            Message += key + "匹配成功;";
                            Failure = false;
                        } else {
                            Message += key + "匹配不成功,数据库值:" + dbdvalue + ",响应值:"+resvalue;
                            FailureMessage += Message;
                            Failure = true;
                            log.info(Message);
                            break;
 
                        }
 
                        log.info("value:" + res_json_obj.get(key));
                    }
                }
 
            } else {
                Message += "接口响应与数据库数据不匹配;";
                 Message += "响应数据:" + res_json_array + "|数据库查询结果:" + dbd_json_array;
                 FailureMessage += Message;
                Failure = true;
                log.info("value:" + res_json_obj.get(key));
                break;
            }
 
        } else {
            FailureMessage = "返回数据为空";
            log.info(FailureMessage);
            Failure = true;
            return;
        }
View Code

 

import org.json.JSONArray;
import org.json.JSONObject;


res_str = prev.getResponseDataAsString();
JSONObject res_json_obj = new JSONObject(res_str);
JSONArray dbd_json_array = new JSONArray("${dbd}"); // 数据库转json
JSONObject dbd_json_obj = new JSONObject();
FailureMessage = "";
Failure = true;

res_json_obj = new JSONObject(res_json_obj.get("data").toString());
if (!res_json_obj.isNull("list")) {
JSONArray res_json_array = new JSONArray(res_json_obj.get("list").toString());
log.info("响应长度:" + res_json_array.length());
log.info("数据返回长度:" + dbd_json_array.length());

if (res_json_array.length() == dbd_json_array.length()) {
for (int i = 0; i < dbd_json_array.length(); i++) {
Message = "\n第"+i+"条记录:";
res_json_obj = res_json_array.getJSONObject(i);
dbd_json_obj = dbd_json_array.getJSONObject(i);

System.out.println(res_json_obj);

Iterator keys = dbd_json_obj.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
String resvalue="";
String dbdvalue="";

System.out.println("key:" + key);
if (key.equals(null) || key.equals("")) {
Message += key + "不存在;";
FailureMessage += Message;
Failure = true;
log.info(Message);
break;
}


if(res_json_obj.has(key) && dbd_json_obj.has(key)) {
resvalue = res_json_obj.getString(key);
dbdvalue = dbd_json_obj.getString(key);
}else {
Message += key + "不存在;";
FailureMessage += Message;
Failure = true;
log.info(Message);
break;
}



if (resvalue.equals(dbdvalue)) {
Message += key + "匹配成功;";
Failure = false;
} else {
Message += key + "匹配不成功,数据库值:" + dbdvalue + ",响应值:"+resvalue;
FailureMessage += Message;
Failure = true;
log.info(Message);
break;

}

log.info("value:" + res_json_obj.get(key));
}
}

} else {
Message += "接口响应与数据库数据不匹配;";
Message += "响应数据:" + res_json_array + "|数据库查询结果:" + dbd_json_array;
FailureMessage += Message;
Failure = true;
log.info("value:" + res_json_obj.get(key));
break;
}

} else {
FailureMessage = "返回数据为空";
log.info(FailureMessage);
Failure = true;
return;
}
这个脚本有个前提,先建立了返回值的对象,当通过jmeter从数据库获得需要验证的数值的时候,需要指定数据库查询返回值字段名与java对象字段名一致。

遇到Typed variable declaration : Object constructor

发现数据库结果数组如下:

dbd=[{cardType=1, cardTypeText=, cardNo=62260906, bankType=1}]

 

原因是cardTypeText无值,将cardTypeText赋值可以解决。

 

相同的代码在eclipse中运行没有问题,但是在jmeter中有问题,可能是对空值的默认处理方法不一样。
---------------------
作者:谁知道丶
来源:CSDN
原文:https://blog.csdn.net/chyo098/article/details/82151019
版权声明:本文为博主原创文章,转载请附上博文链接!

jmeter beanshell Typed variable declaration : Object constructor错误

标签:数据   view   错误   iterator   new   运行   bsp   pen   ati   

原文地址:https://www.cnblogs.com/a00ium/p/10372146.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!