Search code examples
jmeterperformance-testingload-testingjmeter-3.2blazemeter

JMeter - If Controller not working for certain string contains condition?


Background:
I have an API that returns a response like so:

{
  "status": 1,
  "errorCode": null,
  "message": null,
  "data": [
    {
      "id": 33,
      "snapshotId": 2,
      "ceId": 29,
      "month": "Feb",
      "corpRcvPayAmt": 100000,
      "wthRcvPayAmt": -90000
    },
    {
      "id": 31,
      "snapshotId": 2,
      "ceId": 29,
      "month": "Jan",
      "corpRcvPayAmt": 0,
      "wthRcvPayAmt": 0
    }
  ]
}

The data node can vary from 0 size to 12 (empty to 1 per month). The only constant field for data is the "month" node. Other field values are changing.

Goal:
I want to do either a PUT or a POST request, depending on whether or not a specific month is already present in data.

For this, I'm capturing the entire data field into a variable using a Regular Expression Extractor like this: enter image description here

With the sample response above, calling ${data} will give me {"id":33,"snapshotId":2,"ceId":29,"month":"Feb","corpRcvPayAmt":100000.00,"wthRcvPayAmt":-90000.00},{"id":31,"snapshotId":2,"ceId":29,"month":"Jan","corpRcvPayAmt":0.00,"wthRcvPayAmt":0.00}

Now using this field, I just want to do a simple if condition like so: if ${data} contains ${month} do something (PUT request). I'm currently trying to do it like this:

enter image description here

Problem:
The condition ${__groovy("${data}".contains("Jan"))} does not seem to work. I've already tried doing the following:

  • change the condition to ${__groovy("${month}".contains("Jan"))} just to see if the syntax is correct. This works just fine.
  • Add a debug sampler inside the if controller using the condition ${__groovy("${month}".contains("Jan"))} to print ${data}. This also prints the data as expected.enter image description here
  • I also tried using various js syntax for the if condition but so far groovy is the ongiving the best results.

    With this I can confirm the following:

  • ${month} and ${data} are extracted and stored properly
  • groovy syntax for string contains is working properly
  • for some reason, the condition ${__groovy("${data}".contains("Jan"))} is not working.

    What am I missing here? Thanks in advance.


  • Solution

  • Don't reference JMeter Functions and/or Variables in Groovy scripts as:

    1. There could be a conflict with GString Template
    2. For JSR223 Test Elements it causes compilation cache feature malfunction reducing the performance
    3. Your variable might simply resolve into something which will cause Groovy script compilation failure and/or malfunction. You can check jmeter.log file for the actual error details.

    So instead of using ${data} go for vars.get("data") instead, vars is a shorthand for JMeterVariables class instance and this is the best way of getting a JMeter Variable value from the Groovy script.

    The whole function should look like:

    ${__groovy(vars.get('data').contains("Jan"),)}
    

    Also be aware that it's better to use JSON Extractor for obtaining values from JSON responses.