1
votes

Hey I'm doing some beanshell scripting for API testing in jmeter. I've written quite a few jmeter scripts with beanshell and it works fine when using Integer.parseInt() method invocation, but I have a value with decimal places where my SQL returns a value of 20.00000 and my Json path extractor gets 20.0 so my test fails when comparing it. Because of this problem I decided to compare this values as double variables instead of Strings but I'm getting the error bellow when using Double.parseDouble on BeanShell.

2016/08/17 12:48:45 ERROR - jmeter.util.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: print("Width Assertion..."); int Total_Printers_SQL = Integer.parseInt(vars.get . . . '' : Typed variable declaration : Method Invocation Double.parseDouble

2016/08/17 12:48:45 WARN - jmeter.assertions.BeanShellAssertion: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval Sourced file: inline evaluation of: print("Width Assertion..."); int Total_Printers_SQL = Integer.parseInt(vars.get . . . '' : Typed variable declaration : Method Invocation Double.parseDouble

Even with the errors displayed the value of the double variable is printed on Jmeter prompt as you can see below.

enter image description here

If anyone's a beanshell expert and could help me identify the error, that'd be awesome. Thanks!

1
Try with Double.parseDouble(vars.get("Printer_Width_SQL_" + i).toString())... It might need "to be told" it's a string.Iske

1 Answers

2
votes

If the number you get is not a valid Double (1.2s for example, or just null), you will get such exception. The cure is either checking that the value is double by RegEx, or simply trying to parse, and catching exception (note that Beanhell does not pass exceptions properly, so you will have to check for any exception, so it's better to limit it to that one line):

double x = 0.0; // default value
String value = vars.get("myVar");
try
{
    x = Double.parseDouble(value);
}
catch(Exception e)
{
    log.info("Cannot parse " + value + " as double");
}