BeanShell支持java对象的调用,所以可直接通过BeanShell来调用java接口程序。
BeanShell调用java接口例子
1、先编写两个简单的java类
创建java简单工程,并创建BeanUtil接口和它的实现类BeanShellEg,代码如图
package com.seling.test;public interface BeanUtil { public String getUserName(); public String getAddress(); public int mul(int i);}
package com.seling.test;public class BeanShellEg implements BeanUtil{ private String userName; private String address; public BeanShellEg(String name){ this.userName=name; } public BeanShellEg(String name,String address){ this.userName=name; this.address=address; } public String getUserName(){ return userName; } public void setUserName(String userName){ this.userName=userName; } public String getAddress(){ return address; } public void setAddress(String address){ this.address=address; } public int mul(int i){ return i*i; }}
2、在IntelliJ IDEA的Project structure-Artifacts配置打包为jar,并在Build-Build Project
(1)Project structure-Artifacts
(2)一直点OK即可
(3)Build-Build Project ,构建项目
(4)打开步骤(2)的D:\work\jmetertest\out\artifacts\jmetertest_jar,拷贝jmetertest.jar到jmeter的D:\apache-jmeter-3.2\lib\ext下
3、BeanShell脚本编写
创建jmeter线程组,依次新增测试计划、BeanShell Sampler、Java请求、Debug Sampler、察看结果树
(1)测试计划,导入引用包
(2)配置BeanShell Sampler
在BeanShell Sampler中直接实例化BeanShellEg类,并在参数中传入‘seling china’
import com.seling.test.BeanUtil;import com.seling.test.BeanShellEg;BeanUtil bu=new BeanShellEg(bsh.args[0],bsh.args[1]);log.error(bu.getUserName());log.error(bu.getAddress());vars.put("userName",bu.getUserName());vars.put("address",bu.getAddress());
(3)Java请求,Label填写为${userName}
(4)Debug Sampler默认即可
(5)点击运行,在察看结果树看到java请求的标签名字取到了${userName}=seling
(6)察看Debug Sampler实例,
address=china
userName=seling
到此已经学习了一个简单的实例,你学会了吗?