[Jmeter test] how to use bean shell assertion to judge the corresponding result of Json returned by the request

Keywords: Java JSON shell

  • Script structure

    In the above figure, queryMaterialApiDTOListByPkIds is the request that returns the response result in Json format, and then add the bean shell assertion to determine in detail whether the value in the Json result is correct.

  • The corresponding results of Json scheme

     1 {
     2     "code": 0,
     3     "msg": "success",
     4     "success": true,
     5     "data": [
     6         {
     7             "materialCode": "6902265111719",
     8             "materialName": "Steamed fish and soy sauce 450 ml*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 Automatic material conversion",
    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": "Haitian soy sauce 230 g*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 Automatic material conversion",
    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 }

     

     

  • Bean shell script

     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("Steamed fish and soy sauce 450 ml*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("Haitian soy sauce 230 g*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. Get the return result of the response through prev.getResponseDataAsString, and then parse the returned result through org.json.JSONObject and org.json.JSONArray.
    2. After parsing the desired Json object, compare each value to be detected with the expected value in the for loop. If it is not correct, set Failure to true and FailureMessage to specific error information.
    3. It is possible that the pkId value at the beginning of the for loop is inconsistent with the expectation, so it is necessary to calculate the count of the next traversal. If the count is inconsistent with the expectation, it means that the number of response results and expected results are inconsistent.

Posted by deadimp on Mon, 02 Dec 2019 11:51:37 -0800