我试图在 JMeter 中将字符串解析为整数,但由于以下错误而失败。如果我尝试打印 vars.get 返回的字符串,它们看起来不错。
2014/06/28 00:08:52 WARN - jmeter.assertions.BeanShellAssertion: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: ``if (ResponseCode != null && ResponseCode.equals ("200") == false ) { int i = In . . . '' : Typed variable declaration : Method Invocation Integer.parseInt
以下是我的代码
if (ResponseCode != null && ResponseCode.equals ("200") == false )
{
int i = Integer.parseInt(vars.get("currentPMCount"));
int j = Integer.parseInt(vars.get("pmViolationMaxCount"));
if( i > j ){
log.warn("PM count on server is greater than max allowed count.");
}
log.warn( "The return code is " + ResponseCode); // this goes to the JMeter log file
}
else
{
Failure=true ;
FailureMessage = "The response data size was not as expected" ;
}
您的代码看起来不错,但可能存在问题currentPMCount
and/or pmViolationMaxCount
变量。
如果它们确实看起来不错并且看起来像整数并且不超过整数的最大/最小值,您可以尝试以下操作:
-
确保数值周围没有“空格”字符,因为前导或尾随空格将导致转换失败。也许调用trim()
变量方法可以帮助:
int i = Integer.parseInt(vars.get("currentPMCount").trim());
- 如果您将脚本存储到文件中,然后在 Beanshell 断言中提供该文件的路径,您将得到“有问题的”行号
-
我最喜欢的:将代码放入 try/catch 块中,如下所示:
try{
//your code here
}
catch (Exception ex){
log.warn("Error in my script", ex);
throw ex; // elsewise JMeter will "swallow" the above exception
}
这样你会得到更多信息而不是糟糕的堆栈跟踪Error invoking bsh method
消息什么也没说。
See 如何使用 BeanShell:JMeter 最喜欢的内置组件 http://blazemeter.com/blog/queen-jmeters-built-componentshow-use-beanshell指南以获取更多提示和技巧。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)