zoukankan      html  css  js  c++  java
  • jmeter断言之Beanshell断言(判断数据库结果是否符合预期)

    jmeter断言之Beanshell断言(判断数据库结果是否符合预期)

    该篇文章主要讲一下beanshell断言处理数据库结果。

    (一)首先需要添加配置原件JDBC Connection Configuration连接数据库信息,然后发送jdbc请求获取预期结果。我现在使用得是result_variable_name获取得是响应结果集。接下来对数据库结果集对判断。

     (二)解析结果集,对结果集做判断

    2.1:获取数据库对象,然后需要明确下该对象的类型,打印的结果如下:为ArrayList类型。

    Object dbInfo = vars.getObject("dbinfo");    //数据库对象
    log.info("----数据库结果类型:" + dbInfo.getClass().toString());
    

    2021-01-22 17:48:41,004 INFO o.a.j.u.BeanShellTestElement: ----数据库结果类型:class java.util.ArrayList

    2.2:接口响应结果与数据库查询结果做对比,符合条件则判断通过,不符合条件则判断失败

    import java.util.ArrayList;
    import java.util.HashMap;
    import java.util.Iterator;
    import java.util.List;
    import java.util.Map;
    import java.util.Set;
    
    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONArray;
    import com.alibaba.fastjson.JSONObject;
    
    //获取请求的返回值
    String response_data = prev.getResponseDataAsString();
    
    JSONObject responseObj = JSON.parseObject(response_data);   // 响应对象
    JSONArray currentBeans = responseObj.getJSONArray("current_beans");
    
    ArrayList dbInfo = vars.getObject("dbinfo");    //数据库对象
    Map dbInfoMap = new HashMap();
    for(int i = 0; i < dbInfo.size(); i++){
    	Map beans = (Map) dbInfo.get(i);
    	dbInfoMap.put(beans.get("customer_serial_no"), beans);
    }
    
    for(int i = 0; i < currentBeans.size(); i++){
    	JSONObject currentBean = currentBeans.getJSONObject(i);
    	String customerSerialNo = currentBean.getString("customer_serial_no");
    	Map beans = (Map) dbInfoMap.remove(customerSerialNo);
    	if(beans == null){
    		log.info("流水:" + customerSerialNo + " 返回的数据在库中不存在");
    	}
    	
    	//判断相同key的数据同dbInfo
    	Set currentBeanKeySet = currentBean.keySet();
    	Iterator iterator = currentBeanKeySet.iterator();
    	String currentBeanKey;
    	while(iterator.hasNext()){
    		currentBeanKey = (String) iterator.next();
    		Object currentBeanValueObj = currentBean.get(currentBeanKey);
    		String currentBeanValue = currentBeanValueObj == null ? "null" : String.valueOf(currentBeanValueObj);
    		Object dbInfoValueObj = beans.get(currentBeanKey);
    		String dbInfoValue = dbInfoValueObj == null ? "null" : String.valueOf(dbInfoValueObj);
    		
    		if(!currentBeanValue.equals(dbInfoValue)){
    			log.info("customer_serial_no: "+ customerSerialNo +" 的流水数据字段["+ currentBeanKey +"]不一致,接口返回:" + currentBeanValue + " , 数据库为: " + dbInfoValue);
    		}
    	}
    }
    
    if(!dbInfoMap.isEmpty()){
    	log.info("接口未返回的数据库数据:" + JSON.toJSONString(dbInfoMap));
    }
    
  • 相关阅读:
    向工信部投诉中国联通、移动、电信等运营服务商的权威途径
    如何把本机Sql Sever数据库转移到虚拟主机sql数据库
    SQL Server 2005如何远程连接数据库?
    mssql server 2005还原数据库bak文件与“备份集中的数据库备份与现有的xx数据库不同”解决方法
    傲游5里保存的网址,在傲游4不能同步?外加几句吐槽
    mysql数据库基础的简单操作指南
    MVC框架模式技术实例(用到隐藏帧、json、仿Ajax、Dom4j、jstl、el等)
    Web---JSTL(Java标准标签库)-Core核心标签库、I18N国际化、函数库
    Jupyter Notebook导入自定义模块时ImportError
    Pandas数据处理(2): 数据透视表,行转列、列转行、以及一行生成多行
  • 原文地址:https://www.cnblogs.com/qiaoli0726/p/13854396.html
Copyright © 2011-2022 走看看