0
votes

i have this scheme in JMeter:

Get the balance value (GET) put in var mainBalance
Place a recharge (POST)
Verify resulted balance (GET) - put in var updatedBalance such as updatedBalance is mainBalance + 10$
All values must be type float.

Im stuck at the last step: I've put a BeanShell assertion but doesn't work. It think that i get the values in the wrong way and also i dont do the calculation as JMeter wants. I also tried with vars.get(String.valueOf("mainBalance"))

float a = new float(vars.get("mainBalance"));
float b = new float(vars.get("updatedBalance"));
if(b != (a + 10)) {
    Failure = true;
}

This is the log error:

Assertion error: true Assertion failure: false Assertion failure message: org.apache.jorphan.util.JMeterException: Error invoking bsh method: eval In file: inline evaluation of: ``float a = new float("mainBalance"); float b = new float("updatedBalance"); if(b . . . '' Encountered "(" at line 1, column 20.

1

1 Answers

1
votes
  1. Don't use Float data type to represent money operations, go for at least BigDecimal instead
  2. Don't use Beanshell as it might become a performance bottleneck, go for JSR223 Assertion instead.

The relevant code would be something like:

def a = new BigDecimal(vars.get('mainBalance'))
def b = new BigDecimal(vars.get('updatedBalance'))
if (b.compareTo(a.add(new BigDecimal('10'))) != 0) {
    AssertionResult.setFailure(true)
}

More information: Scripting JMeter Assertions in Groovy - A Tutorial