beanshell Sampler引入jar测试成功,JSR223 Sampler 失败记录

同样的jar包使用beanshell Sampler可以测试,使用JSR223 Sampler报错

Java方法导出jar包,使用测试计划添加,或放入lib包,或ext包下

public class Aal {
    public int  add(int a,int b){
        return a+b;
    }

}

jar包内容:

 

 

 

jmeter beanshell Sampler脚本

import Aal;
Aal aal = new Aal(); int res=aal.add(1,5); log.info("***********************"+res);

beanshell Sampler调用测试

日志输出:

INFO o.a.j.u.BeanShellTestElement: ***********************6

JSR223 Sampler 调用测试

日志输出:说明引入的jar包有问题

 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: Sourced file: inline evaluation of: ``Aal aal = new Aal(); int res=aal.add(1,5); log.info("***********************"+re . . . '' : Typed variable declaration : Class: Aal not found in namespace : at Line: 1 : in file: inline evaluation of: ``Aal aal = new Aal(); int res=aal.add(1,5); log.info("***********************"+re . . . '' : Aal 
 in inline evaluation of: ``Aal aal = new Aal(); int res=aal.add(1,5); log.info("***********************"+re . . . '' at line number 1
javax.script.ScriptException: Sourced file: inline evaluation of: ``Aal aal = new Aal(); int res=aal.add(1,5); log.info("***********************"+re . . . '' : Typed variable declaration : Class: Aal not found in namespace : at Line: 1 : in file: inline evaluation of: ``Aal aal = new Aal(); int res=aal.add(1,5); log.info("***********************"+re . . . '' : Aal 
 in inline evaluation of: ``Aal aal = new Aal(); int res=aal.add(1,5); log.info("***********************"+re . . . '' at line number 1
	at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:93) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) ~[bsh-2.0b6.jar:2.0b6 2016-02-05 05:16:19]
	at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) ~[java.scripting:?]
	at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:219) ~[ApacheJMeter_core.jar:5.4.1]
	at org.apache.jmeter.protocol.java.sampler.JSR223Sampler.sample(JSR223Sampler.java:72) [ApacheJMeter_java.jar:5.4.1]
	at org.apache.jmeter.threads.JMeterThread.doSampling(JMeterThread.java:638) [ApacheJMeter_core.jar:5.4.1]
	at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:558) [ApacheJMeter_core.jar:5.4.1]
	at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.4.1]
	at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.4.1]
	at java.lang.Thread.run(Thread.java:834) [?:?]

 重写打包测试成功

 

 JSR223 Sampler 调用测试,日志输出成功

import Test.*;
Add add = new Add();
int res=add.add(1,5);
//System.out.println("*********res:"+res);
log.info("***********************"+res);

 

 

总结:

1、打完jar后,查看jar里面是class文件,还是java文件,java 用JSR223 Sampler可能引用不到;

2、用JSR223 Sampler时,注意打jar包时的类文件在一个package下面,没有package用JSR223 Sampler可能引用不到;

3、测试计划加入jar后,在JSR223 Sampler中要引入,不引入JSR223 Sampler可能找不到类;

4、有时候引入jar后,可能要重启jmeter;

 

 

 

JSR223 Sampler 直接写方法测试

public class Aal {
    public int  add(int a,int b){
        return a+b;
    }

}
Aal aal = new Aal();
int res1=aal.add(1,5);
log.info("********************************************res1:"+res1);

日志输出:

INFO o.a.j.p.j.s.J.JSR223 Sampler: ********************************************res1:6

 

posted @ 2022-07-22 22:48  颓废且努力奋斗的人  阅读(1338)  评论(0编辑  收藏  举报