【Jmeter測試】如何使用BeanShell斷言判斷請求返回的Json相應結果

  • 腳本結構

    上圖中,queryMaterialApiDTOListByPkIds是返回Json格式響應結果的請求,然後添加BeanShell斷言詳細判斷Json結果中的值是否正確。

  • Json格式的相應結果

    {
    	"code": 0,
    	"msg": "success",
    	"success": true,
    	"data": [
    		{
    			"materialCode": "6902265111719",
    			"materialName": "海天蒸魚豉油450ml*12",
    			"materialType": 1,
    			"sixNineCode": "6902265111719",
    			"expirationDate": 720,
    			"packingSpecification": 12,
    			"basicUnit": "BOX",
    			"minSaleUnit": "BOT",
    			"importFlag": 1,
    			"transportFlag": 0,
    			"sourceSystem": 10,
    			"createrName": "MD自動轉換物料",
    			"updaterName": "loms",
    			"pkId": "6902265111719",
    			"mdMaterialCode": "6902265111719",
    			"verifyStatus": 2,
    			"creater": -2,
    			"createTime": 1538984955619,
    			"updater": -2,
    			"updateTime": 1538984955619,
    			"useStatus": 1
    		},
    		{
    			"materialCode": "6902265113003",
    			"materialName": "海天特辣黃豆醬230g*15",
    			"materialType": 1,
    			"sixNineCode": "6902265113003",
    			"expirationDate": 720,
    			"packingSpecification": 15,
    			"basicUnit": "BOX",
    			"minSaleUnit": "BOT",
    			"importFlag": 1,
    			"transportFlag": 0,
    			"sourceSystem": 10,
    			"createrName": "MD自動轉換物料",
    			"updaterName": "loms",
    			"pkId": "6902265113003",
    			"mdMaterialCode": "6902265113003",
    			"verifyStatus": 2,
    			"creater": -2,
    			"createTime": 1538984956726,
    			"updater": -2,
    			"updateTime": 1538984956726,
    			"useStatus": 1
    		}
    	],
    	"EnumVal": {}
    }

     

  • BeanShell腳本

    
    import org.json.JSONObject;
    import org.json.JSONArray;
    
    String result = prev.getResponseDataAsString();
    
    JSONObject response = new JSONObject(result);
    JSONArray array = response.getJSONArray("data");
    
    if (array.length() != 2) {
      Failure=true ;
      FailureMessage ="array size < 2";
      return;
    }
    
    int count = 0;
    for (int i = 0; i < 2; i++) {
      JSONObject temp = array.getJSONObject(i);
      String pkId = temp.get("pkId").toString();
      if (pkId.equals("6902265111719")) {
      	
          if (!temp.get("materialCode").equals("6902265111719")) {
              Failure=true ;
              FailureMessage ="pkId: " + pkId + ", material code error, code = " + temp.get("materialCode");
              return;
          }
    
          if (!temp.get("materialName").equals("海天蒸魚豉油450ml*12")) {
              Failure=true ;
              FailureMessage ="pkId: " + pkId + ", material name error, name = " + temp.get("materialName");
              return;
          }
          count++;
      }
    
      if (pkId.equals("6902265113003")) {
          if (!temp.get("materialCode").equals("6902265113003")) {
              Failure=true ;
              FailureMessage ="pkId: " + pkId + ", material code error, code = " + temp.get("materialCode");
              return;
          }
    
          if (!temp.get("materialName").equals("海天特辣黃豆醬230g*15")) {
              Failure=true ;
              FailureMessage ="pkId: " + pkId + ", material name error, name = " + temp.get("materialName");
              return;
          }
          count++;
      }
    }
    
    if (count != 2) {
      log.info("count != 2");
      Failure=true ;
      FailureMessage ="pkId not in range";
      return;
    }
    
    log.info(array.toString())
    
    

    1、先通過prev.getResponseDataAsString獲取到響應的返回結果,然後通過org.json.JSONObject和org.json.JSONArray兩個類來解析返回的相應結果。
    2、解析出想要的Json對象後,在for循環中對Json對象中每一個需要檢測的值和期望的進行比對,如果不正確,Failure設置爲true,FailureMessage設置具體的錯誤信息。
    3、for循環中有可能一開始的pkId取值就和期望不一致,所以這時需要計算下遍歷的計數count,如果計數和期望的不一致,說明響應結果和期望結果的數量不一致。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章