51Testing软件测试论坛

标题: 如何判断接口响应结果(复杂json)是否和数据库查询结果一致 [打印本页]

作者: liuheng1    时间: 2017-2-21 17:25
标题: 如何判断接口响应结果(复杂json)是否和数据库查询结果一致
接口响应数据为{"content":{"appList":[{"appSummary":{"appName":"智能厅堂系统","categoryId":1001002,"groupId":1,"groupName":"综合业务","l1CateName":"应用群组","l2CateName":"生产应用系统","l3CateName":"规划类","objectId":70466},"attention":false,"healthDegree":100},{"appSummary":{"appName":"综合柜面系统","categoryId":1001002,"groupId":1,"groupName":"综合业务","l1CateName":"应用群组","l2CateName":"生产应用系统","l3CateName":"规划类","objectId":70512},"attention":false,"healthDegree":100},{"appSummary":{"appName":"综合业务系统","categoryId":1001002,"groupId":1,"groupName":"综合业务","l1CateName":"应用群组","l2CateName":"生产应用系统","l3CateName":"规划类","objectId":70211},"attention":false,"healthDegree":100},{"appSummary":{"appName":"规则引擎","categoryId":1001002,"groupId":1,"groupName":"综合业务","l1CateName":"应用群组","l2CateName":"生产应用系统","l3CateName":"规划类","objectId":70219},"attention":false,"healthDegree":100},{"appSummary":{"appName":"PE流程引擎","categoryId":1001002,"groupId":1,"groupName":"综合业务","l1CateName":"应用群组","l2CateName":"生产应用系统","l3CateName":"规划类","objectId":70221},"attention":false,"healthDegree":100},{"appSummary":{"appName":"旧版网上银行系统","categoryId":1001002,"groupId":1,"groupName":"综合业务","l1CateName":"应用群组","l2CateName":"生产应用系统","l3CateName":"规划类","objectId":70355},"attention":false,"healthDegree":100},{"appSummary":{"appName":"微信银行系统","categoryId":1001002,"groupId":1,"groupName":"综合业务","l1CateName":"应用群组","l2CateName":"生产应用系统","l3CateName":"规划类","objectId":70373},"attention":false,"healthDegree":100},{"appSummary":{"appName":"电话银行系统","categoryId":1001002,"groupId":1,"groupName":"综合业务","l1CateName":"应用群组","l2CateName":"生产应用系统","l3CateName":"规划类","objectId":70391},"attention":false,"healthDegree":100},{"appSummary":{"appName":"村镇银行综合业务系统","categoryId":1001002,"groupId":1,"groupName":"综合业务","l1CateName":"应用群组","l2CateName":"生产应用系统","l3CateName":"规划类","objectId":70327},"attention":false,"healthDegree":100}]},"status":true}
BeanShell断言目的:想要判断上面接口响应结果是否和数据库查询结果select s.object_name,s.cate_id,g.group_id,g.group_name,s.l1_cate_name from cmdb_app_group g,cmdb_app_group_detail d,cmdb_object_summary s where g.group_id=d.group_id and d.object_id=s.object_id and g.group_id="+objid+"一致
BeanShell断言内容:
import java.sql.*;  
import java.util.*;  
import java.lang.*;  
import org.apache.regexp.*;  
import com.google.gson.JsonObject;   
import com.google.gson.JsonParser;   



//数据库连接字段  
String drive = "com.mysql.jdbc.Driver";  
String url = "jdbc:mysql://10.9.3.210:3306";  
String dbName = "camadev";  
String user = "root";  
String pass = "root";  


String history = "";  
String response = "";  
String failuer = "";  

//vars.get是Jmeter提供的方法,可以取到变量值,这个caseno是用来关联用例和数据库中结果的  
String objId = vars.get("objId");  

//下面是查询的SQL  
String query="select s.object_name,s.cate_id,g.group_id,g.group_name,s.l1_cate_name from cmdb_app_group g,cmdb_app_group_detail d,cmdb_object_summary s where g.group_id=d.group_id and d.object_id=s.object_id and g.group_id="+objid+"";

//JDBC声明  
Connection Mycon = null;  
Statement Mystmt = null;  
ResultSet Myrset = null;  

//try中获取数据库连接  
try{  
        Mycon = DriverManager.getConnection("jdbc:mysql://10.9.3.210:3306/camadev", "root", "root");  

}   catch(SQLException e){  

}  
Mystmt = Mycon.createStatement();  
Myrset = Mystmt.executeQuery(query);  


//prev.getResponseDataAsString是Jmeter提供的方法,可以调取上次请求的响应字符串  
response = prev.getResponseDataAsString();  

//如果取到库中的数据,赋值给history  
while (Myrset.next()){  
        history = Myrset.getString(1);  
}  

Myrset.close();  
Mystmt.close();  


//Gson提供的方法,原理笔者也不明白,效果是把字符串生成Json对象  

JsonParser parser = new JsonParser();   
JsonObject responseObj = (JsonObject) parser.parse(response);   
JsonParser parser1 = new JsonParser();            
JsonObject historyObj = (JsonObject) parser1.parse(history);   


if(history == "")  
{  
        Failure = true;  
        FailureMessage = "连接数据库失败或者数据库内没有历史数据";   

//调用Gson提供的Json对象euqals方法判断是否一致  
}else if(responseObj.equals(historyObj) == false)  
{   
        //把断言失败置为真   
Failure = true;  
FailureMessage = "和历史数据不匹配";   
        }  
else{
                //把断言失败置为真   
Failure = false;  
FailureMessage = "和历史数据匹配";   
}

执行后BeanShell断言报错信息:Assertion error: true
Assertion failure: false
Assertion failure message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval        Sourced file: inline evaluation of: ``import java.sql.*;   import java.util.*;   import java.lang.*;   import org.apac . . . ''
请问大师们,怎样调试我的BeanShell断言,或者有什么别的简单方法。





欢迎光临 51Testing软件测试论坛 (http://bbs.51testing.com/) Powered by Discuz! X3.2