简体   繁体   中英

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.

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))
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM