1

I'm trying to evaluate a not equals in a if condition in bean shell but though the logic seems to be correct. I'm not getting the expected results.

This is for bean shell post processor in jmeter

r = ctx.getPreviousResult().getResponseCode();

if (!r.equals(200))
{
  log.info("vin IS --> "+"${vin}");
  p.println(r +","+ "${vin}" + ",");
}

I'm intending to print only non 200 response code but it prints 200 response codes too.

thanks in advance for your help

Varun
  • 117
  • 1
  • 4
  • 14
  • @UBIKLOADPACK Apologies!! I didn't change the accepted answer. I thought i could accept both the answers but i was wrong. I'll revert it. – Varun Jan 23 '19 at 09:56

2 Answers2

1

The code :

if (!r.equals(200))

Should be:

if (!r.equals("200"))

And by the way, you should not use Beanshell anymore, prefer JSR223 Test Elements + Groovy as per this :

UBIK LOAD PACK
  • 33,980
  • 5
  • 71
  • 116
1
  1. You're comparing a String with an Integer, you need to either cast it to the Integer first like:

    r = Integer.parseInt(ctx.getPreviousResult().getResponseCode());
    
  2. You're using Beanshell which is a some form of performance anti-pattern. It's recommended to use JSR223 Test Elements and Groovy language for any form of scripting as Groovy has much better performance comparing to Beanshell.
  3. You're inlining JMeter Variables into scripts, it is not very safe as variables might resolve into something which cause compilation failure or unexpected behavior. Moreover in case of Groovy variables will either be resolved only once or clash with GString templates / compilation caching feature. So consider changing:

    log.info("vin IS --> "+"${vin}");
    

    to

    log.info("vin IS --> "+vars.get("vin"));
    
Dmitri T
  • 159,985
  • 5
  • 83
  • 133