Get the App
SLTechnology News&Howtos  ›  Internet Technology  › 

Beanshell script asserts Mock interface

Shulou Source: shulou.com Published: 2022-06-03 02:50:50 09月13日 Update

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

Tags: Interface script different complex information at the same time name processor field log time service provider level result such as figure processing service test adjustment Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Microsoft Apple Shulou Information macOS NVidia