咱們在作接口斷言的時候,若是遇到複雜的json,能夠考慮用beanshell腳本去解析list,同時加入自定義的斷言shell
Mock例:https://www.easy-mock.com/mock/5cb46739edfdac7de69fe792/example/providerapache
如圖所示,咱們在json返回中有name和remark字段,但願斷言remark和name的返回值。若是斷言失敗,拋出自定義的異常信息json
咱們在mock接口下添加一個後置處理器的beanshellide
import org.apache.jmeter.assertions; import org.apache.jmeter.samplers.SampleResult; import org.apache.jmeter.assertions.AssertionResult; import org.json.*; String response_data = prev.getResponseDataAsString(); JSONObject data_obj = new JSONObject(response_data); log.info(response_data); String code = data_obj.get("remark"); log.info(code); if(code.equals("接口測試服務商")) { String user_name = data_obj.get("dockedContact").get("name").toString(); if(user_name.equals("zhufc")){ Failure = true; log.info(user_name); log.info("提取值與預期相符,斷言經過"); FailureMessage = "斷言經過"; } else{ Failure = false; log.info("名稱不一樣,斷言失敗"); FailureMessage = "斷言失敗"; prev.setStopThread(true); } }
啓動腳本,打開log,查看結果測試