Beanshell script asserts Mock interface
When making interface assertions, if we encounter complex json, we can consider parsing list with beanshell script and adding custom assertions at the same time.
Mock example: https://www.easy-mock.com/mock/5cb46739edfdac7de69fe792/example/provider
As shown in the figure, we have name and remark fields in the json return, and we want to assert the return values of remark and name. If the assertion fails, a custom exception message is thrown
We add a beanshell of the post processor under the mock interface
Write assertion script in beanshell
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 ("interface test service provider")) {String user_name = data_obj.get ("dockedContact"). Get ("name"). ToString (); if (user_name.equals ("zhufc")) {Failure = true; log.info (user_name); log.info ("extract value as expected, assertion passed"); FailureMessage = "assertion passed";} else {Failure = false;log.info ("different name, assertion failed") FailureMessage = "assertion failed"; prev.setStopThread (true);}}
Log rating is adjusted to info
Start the script, open log, and view the results