JMeter二次开发(2)-编写 JSON Assertion 插件

本篇文章主要介绍如何对JMeter进行二次开发,添加自己所需的功能。这里以Json验证为例进行说明。在web接口测试过程中,JSON的应用已经非常普遍,但原声的JMeter并没有提供Json及JsonPath的验证,这里以JSON格式验证为例进行JMeter二次开发简单说明。

 

准备工作:1)JMeter本地开发环境;2)gson.jar-用于做json数据有效性验证(也可以自己编写验证逻辑)

 

具体步骤:

1.引用gson.jar

2.添加JSONAssertion及JSONAssertionGui

 

2.1 src/components/org.apache.jmeter.assertions下,新建JSONAssertion

public class JSONAssertion extends AbstractTestElement implements Serializable, Assertion, ThreadListener {
    private static final Logger log = LoggingManager.getLoggerForClass();

    private static final long serialVersionUID = 240L;

    /**
     * Returns the result of the Assertion. Here it checks wether the Sample
     * took to long to be considered successful. If so an AssertionResult
     * containing a FailureMessage will be returned. Otherwise the returned
     * AssertionResult will reflect the success of the Sample.
     */
    @Override
    public AssertionResult getResult(SampleResult response) {
        // no error as default
        AssertionResult result = new AssertionResult(getName());
        String resultData = response.getResponseDataAsString();
       
        if (resultData.length()==0) {
            return result.setResultForNull();
        }else{
            Gson gson = new Gson();
            if(!gson.toJsonTree(resultData).isJsonObject()){
                log.debug("Cannot parse result content"); // may well happen
                result.setFailure(true);
                result.setFailureMessage("ResultData is not Json");
            }
        }
        return result;

    }

    @Override
    public void threadStarted() {
    }

    @Override
    public void threadFinished() {
    }
}

2.2 src/components/org.apache.jmeter.assertions.gui下,新建JSONAssertionGui

public class JSONAssertionGui extends AbstractAssertionGui {
   
    private static final long serialVersionUID = 240L;

    /**
     * The constructor.
     */
    public JSONAssertionGui() {
        init();
    }

    /**
     * Returns the label to be shown within the JTree-Component.
     */
    @Override
    public String getLabelResource() {
        return "json_assertion_title"; // $NON-NLS-1$
    }

    @Override
    public TestElement createTestElement() {
        JSONAssertion el = new JSONAssertion();
        modifyTestElement(el);
        return el;
    }

    /**
     * Modifies a given TestElement to mirror the data in the gui components.
     * 
     * @see org.apache.jmeter.gui.JMeterGUIComponent#modifyTestElement(TestElement)
     */
    @Override
    public void modifyTestElement(TestElement el) {
        configureTestElement(el);
    }

    /**
     * Inits the GUI.
     */
    private void init() {
        setLayout(new VerticalLayout(5, VerticalLayout.BOTH, VerticalLayout.TOP));
        setBorder(makeBorder());

        add(makeTitlePanel());
    }
}

 

3.配置properties文件

在src/core/org/apache/jmeter/resources/下的.properties文件中进行设置

 

 

4.Run As Java Application —>[New Driver]

5.JMeter运行后,新建项目进行验证

 

 

添加Assertion Listener,查看运行结果

 

 

关于新加功能的加载过程:

controllser、samplser、listener、assertion等内容,并不是在代码中new出来的,而是在jmeter启动时,通过扫描工作路径下的class,分析class类型,反射生成到jmeter中的。所以,在写完JSON Assertion的GUI之后,并不需要更改jmeter其他内容。具体的扫描反射过程,在org.apache.jmeter.testbeans.gui.TestBeanGUI中进行。

 

 

完整代码地址

https://github.com/buaawp/jmeter/tree/json_assertion

 

转载于:https://www.cnblogs.com/buaawp/p/5287604.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值