JMeter BeanShell Assertion: Getting error when convert String to Long

Manigandan

Have a need to change the value from String to Long in BeanShell Assertion to do verification.

First Apporach

long balance_after_credit = Long.parseLong(String.valueOf("${balance_after_credit_from_db}"));   

Second Approach

long balance_after_credit = Long.parseLong(vars.get("balance_after_credit_from_db"));

For instance, consider am getting a value as '743432545' for the variable balance_after_credit_from_db.

Error

org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval    Sourced file: inline evaluation of: ``long token_balance_after_credit = Long.parseLong(vars.get("token_balance_after_c . . . '' : Typed variable declaration : Method Invocation Long.parseLong

Weird thing is sometimes, I didn't get errors and the script is getting passed. Can anyone please point out where am doing a mistake. TIA.

Dmitri T
  1. Inlining JMeter variables into code-based scripts is not something recommended so go for 2nd approach.

  2. How do you know that exactly String is being returned from the database all the time? It easily can be any other object type, in fact any of described in the Mapping SQL and Java Types article. The way more safe approach will be something like:

    if (vars.getObject("balance_after_credit_from_db") instanceof String) {
        long balance_after_credit = Long.parseLong(vars.get("balance_after_credit_from_db"));
    } 
    else {
        log.error("Unexpected \balance_after_credit_from_db\" variable type");
        log.error("Expected: String, Actual: " + vars.getObject("balance_after_credit_from_db").getClass().getName());
        throw new Exception("Unexpected variable type");
    }
    

    So in case of non-String JDBC query result you will be able to see the relevant message in jmeter.log file

    JMeter variable type

See Debugging JDBC Sampler Results in JMeter article for more information on working with the entities coming from databases in JMeter tests

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Jmeter - Inline evaluation error in Beanshell Assertion

Jmeter : Error when I try to get data from Database using beanshell assertion

Beanshell type of variable assertion on Jmeter

Encountered "=" error in Jmeter when List is used in jmeter beanshell code

JMeter BeanShell - integer to string

JMeter Beanshell Integer Error

JMeter Assertion : Error when comparing variables

JMeter beanshell string comparison issue

JMeter Thread Group Not Getting to BeanShell PostProcessor

Unable to append to next line in a file through beanshell assertion sampler in Jmeter

Jmeter BeanShell assertion parsing and comparing UTC datetime from json

C: Convert string to unsigned long long Error

JMeter: Count comparison of value fetched from Beanshell Postprocessor, using the Beanshell assertion

Parsing string to integer in BeanShell Sampler in JMeter

Error in Beanshell Sampler JMeter for parsing RegularExpressionExtractor

Error on accessing Output Variable in beanshell preprocessor in Jmeter

Getting error : Could not deserialize object. Failed to convert a value of type java.lang.String to long

when unit testing controller i'm getting an assertion error

evaluating a string of code in beanshell and getting the value being returned by the beanshell interpreter

Jmeter/Groovy: getting iteration number without reference to Beanshell

Unable to convert unicode to string in python getting error

Jmeter : getting 401 error

Getting an error, 'Can't convert from BSON type objectId to String' when using MongoDB aggregation

getting could not convert string to Timestamp error when trying pass pandas series data to date_range fucntion

JMETER Beanshell ( java.lang.String ) not found in class

Getting an error from the application when recording a Jmeter test plan

Requests getting failed with error 400 when thread count increased in jmeter

JMeter / Beanshell "Error invoking bsh method: eval Sourced file:"

JMeter - Exception when using Beanshell as JSR223 Sampler with file