【问题标题】:How to set value for a variable in Jmeter如何在 Jmeter 中为变量设置值
【发布时间】:2018-12-21 01:05:46
【问题描述】:

我正在使用 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 ) 

如何解决?

【问题讨论】:

    标签: jmeter


    【解决方案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); 行,否则您的代码将无法工作。

    【讨论】:

      【解决方案2】:

      您必须将数字变量转换为字符串:

      int a = 1;
      int b = 2;
      int c = a + b;
      vars.put("c", c.toString());
      

      【讨论】:

      • 感谢您的回答
      猜你喜欢
      • 1970-01-01
      • 2018-06-02
      • 1970-01-01
      • 2011-06-12
      • 2014-03-28
      • 2017-01-16
      • 1970-01-01
      • 1970-01-01
      • 2021-04-03
      相关资源
      最近更新 更多