Java – parses strings into integers in the BeanShell sampler in JMeter

I'm trying to parse a string into an integer in JMeter, but I failed due to a follow error If I try to print vars Get returns strings that look good

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

Here is my code

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" ;   
}

Solution

Your code looks good, but it may be a problem with the currentpmcount and / or pmviolationmaxcount variables

If they look good and look like integers and do not exceed the maximum / minimum value of integer, you can try the following methods:

>Make sure there is no "space" character around the numeric value because leading or trailing spaces will cause the conversion to fail Perhaps calling the trim () method on a variable can help:

int i = Integer.parseInt(vars.get("currentPMCount").trim());

>If you store the script in a file and then provide the file path in the BeanShell assertion, the "problematic" line number > my favorite: surround the code in the try / catch block, as follows:

try{
    //your code here
}
catch (Exception ex){
    log.warn("Error in my script",ex);
    throw ex; // elsewise JMeter will "swallow" the above exception
}

In this way, you'll get more informative stack traces than bad error calls to BSH methodmessage, which says nothing

For more tips and tricks, see the how to use BeanShell: JMeter's favorite build in component guide

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>