我正在使用JSR223 Sampler并尝试进行算术运算。
try {
setStrictJava(true);
int a=1;
int b=2;
int c = a+b;
vars.put("c",c);
} catch(Exception ex) {
log.error("something wrong", ex);
throw ex;
}
出现以下错误。
2018-12-18 18:19:59,554 ERROR o.a.j.p.j.s.JSR223Sampler: Problem in JSR223 script JSR223 Sampler, message: javax.script.ScriptException: Sourced file: inline evaluation of: ``try{ setStrictJava(true); int a=1; int b=2; int c = a+b; vars.put("c",c); } catc . . . '' : Error in method invocation: Method put( java.lang.String, int ) not found in class'org.apache.jmeter.threads.JMeterVariables' : at Line: 6 : in file: inline evaluation of: ``try{ setStrictJava(true); int a=1; int b=2; int c = a+b; vars.put("c",c); } catc . . . '' : vars .put ( "c" , c )
in inline evaluation of: ``try{ setStrictJava(true); int a=1; int b=2; int c = a+b; vars.put("c",c); } catc . . . '' at line number 6
javax.script.ScriptException: Sourced file: inline evaluation of: ``try{ setStrictJava(true); int a=1; int b=2; int c = a+b; vars.put("c",c); } catc . . . '' : Error in method invocation: Method put( java.lang.String, int ) not found in class'org.apache.jmeter.threads.JMeterVariables' : at Line: 6 : in file: inline evaluation of: ``try{ setStrictJava(true); int a=1; int b=2; int c = a+b; vars.put("c",c); } catc . . . '' : vars .put ( "c" , c )
如何解决这个问题?
答案 0 :(得分:1)
您要么需要convert your variable to String才能使用vars.put()函数,例如:
vars.put("c", String.valueOf(c));
或改为使用vars.putObject()函数
vars.putObject("c", c);
另外请注意,您应该使用Groovy language in the JSR223 Sampler,在这种情况下,您将必须删除setStrictJava(true);
行,否则您的代码将无法工作。
答案 1 :(得分:0)
您必须将数字变量转换为字符串:
int a = 1;
int b = 2;
int c = a + b;
vars.put("c", c.toString());