Search code examples
scalagatling

How to feed random numbers for the same variable names in Gatling


If you have a JSON, where it has an array of customers where each customer has to have a unique customer number, how can I feed this with random numbers:

    {
        "customers": [
            {
                "customerNo": "123",
                "Name": "Joe"
            },
            {
                "customerNo": "456"
                "Name": "Jane"
            },
        ]
    }

I thought this might work:

    {
        "customers": [
            {
                "customerNo": "${customerNo}",
                "Name": "Joe"
            },
            {
                "customerNo": "${customerNo}"
                "Name": "Jane"
            },
        ]
    }
    val customerNumber = Iterator.continually(
      Map("customerNumber" -> Random.nextInt(Integer.MAX_VALUE))
    )

Then by adding:

    feed(customerNumber)

But this uses the same generated number in both cases.


Solution

  • The cleanest way is to pass a function, eg in Java:

    StringBody(session ->
    """
    {
      "customers": [
        {
          "customerNo": "%s",
          "Name": "Joe"
        },
        {
          "customerNo": "%s"
          "Name": "Jane"
        },
      ]
    }""".formatted(Random.nextInt(Integer.MAX_VALUE), Random.nextInt(Integer.MAX_VALUE))
    )