Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Beanshell script asserts Mock interface

2025-04-21 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/03 Report--

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report