51Testing软件测试论坛

 找回密码
 (注-册)加入51Testing

QQ登录

只需一步,快速开始

微信登录,快人一步

手机号码,快捷登录

查看: 2264|回复: 0
打印 上一主题 下一主题

如何判断接口响应结果(复杂json)是否和数据库查询结果一致

[复制链接]
  • TA的每日心情
    开心
    2017-3-17 10:59
  • 签到天数: 3 天

    连续签到: 2 天

    [LV.2]测试排长

    跳转到指定楼层
    1#
    发表于 2017-2-21 17:25:31 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
    接口响应数据为{"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断言,或者有什么别的简单方法。
    分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
    收藏收藏
    回复

    使用道具 举报

    本版积分规则

    关闭

    站长推荐上一条 /1 下一条

    小黑屋|手机版|Archiver|51Testing软件测试网 ( 沪ICP备05003035号 关于我们

    GMT+8, 2024-11-15 00:38 , Processed in 0.073191 second(s), 23 queries .

    Powered by Discuz! X3.2

    © 2001-2024 Comsenz Inc.

    快速回复 返回顶部 返回列表