【Jmeter测试】如何使用BeanShell断言判断请求返回的Json相应结果

JAVA学习网 2019-01-30 23:01:03
 
  • 脚本结构

    上图中,queryMaterialApiDTOListByPkIds是返回Json格式响应结果的请求,然后添加BeanShell断言详细判断Json结果中的值是否正确。

  • Json格式的相应结果

     1 {
     2     "code": 0,
     3     "msg": "success",
     4     "success": true,
     5     "data": [
     6         {
     7             "materialCode": "6902265111719",
     8             "materialName": "海天蒸鱼豉油450ml*12",
     9             "materialType": 1,
    10             "sixNineCode": "6902265111719",
    11             "expirationDate": 720,
    12             "packingSpecification": 12,
    13             "basicUnit": "BOX",
    14             "minSaleUnit": "BOT",
    15             "importFlag": 1,
    16             "transportFlag": 0,
    17             "sourceSystem": 10,
    18             "createrName": "MD自动转换物料",
    19             "updaterName": "loms",
    20             "pkId": "6902265111719",
    21             "mdMaterialCode": "6902265111719",
    22             "verifyStatus": 2,
    23             "creater": -2,
    24             "createTime": 1538984955619,
    25             "updater": -2,
    26             "updateTime": 1538984955619,
    27             "useStatus": 1
    28         },
    29         {
    30             "materialCode": "6902265113003",
    31             "materialName": "海天特辣黄豆酱230g*15",
    32             "materialType": 1,
    33             "sixNineCode": "6902265113003",
    34             "expirationDate": 720,
    35             "packingSpecification": 15,
    36             "basicUnit": "BOX",
    37             "minSaleUnit": "BOT",
    38             "importFlag": 1,
    39             "transportFlag": 0,
    40             "sourceSystem": 10,
    41             "createrName": "MD自动转换物料",
    42             "updaterName": "loms",
    43             "pkId": "6902265113003",
    44             "mdMaterialCode": "6902265113003",
    45             "verifyStatus": 2,
    46             "creater": -2,
    47             "createTime": 1538984956726,
    48             "updater": -2,
    49             "updateTime": 1538984956726,
    50             "useStatus": 1
    51         }
    52     ],
    53     "EnumVal": {}
    54 }

     

     

  • BeanShell脚本

     1 import org.json.JSONObject;
     2 import org.json.JSONArray;
     3 
     4 String result = prev.getResponseDataAsString();
     5 
     6 JSONObject response = new JSONObject(result);
     7 JSONArray array = response.getJSONArray("data");
     8 
     9 if (array.length() != 2) {
    10   Failure=true ;
    11   FailureMessage ="array size < 2";
    12   return;
    13 }
    14 
    15 int count = 0;
    16 for (int i = 0; i < 2; i++) {
    17   JSONObject temp = array.getJSONObject(i);
    18   String pkId = temp.get("pkId").toString();
    19   if (pkId.equals("6902265111719")) {
    20       
    21       if (!temp.get("materialCode").equals("6902265111719")) {
    22           Failure=true ;
    23           FailureMessage ="pkId: " + pkId + ", material code error, code = " + temp.get("materialCode");
    24           return;
    25       }
    26 
    27       if (!temp.get("materialName").equals("海天蒸鱼豉油450ml*12")) {
    28           Failure=true ;
    29           FailureMessage ="pkId: " + pkId + ", material name error, name = " + temp.get("materialName");
    30           return;
    31       }
    32       count++;
    33   }
    34 
    35   if (pkId.equals("6902265113003")) {
    36       if (!temp.get("materialCode").equals("6902265113003")) {
    37           Failure=true ;
    38           FailureMessage ="pkId: " + pkId + ", material code error, code = " + temp.get("materialCode");
    39           return;
    40       }
    41 
    42       if (!temp.get("materialName").equals("海天特辣黄豆酱230g*15")) {
    43           Failure=true ;
    44           FailureMessage ="pkId: " + pkId + ", material name error, name = " + temp.get("materialName");
    45           return;
    46       }
    47       count++;
    48   }
    49 }
    50 
    51 if (count != 2) {
    52   log.info("count != 2");
    53   Failure=true ;
    54   FailureMessage ="pkId not in range";
    55   return;
    56 }
    57 
    58 log.info(array.toString())

     

    1、先通过prev.getResponseDataAsString获取到响应的返回结果,然后通过org.json.JSONObject和org.json.JSONArray两个类来解析返回的相应结果。
    2、解析出想要的Json对象后,在for循环中对Json对象中每一个需要检测的值和期望的进行比对,如果不正确,Failure设置为true,FailureMessage设置具体的错误信息。
    3、for循环中有可能一开始的pkId取值就和期望不一致,所以这时需要计算下遍历的计数count,如果计数和期望的不一致,说明响应结果和期望结果的数量不一致。

 
阅读(2759) 评论(0)