Random Responses for the same service call

331 views
Skip to first unread message

Tina B

unread,
Mar 8, 2021, 1:31:56 PM3/8/21
to wiremock-user
Hi- I want to do the mocking of the request- ex: GET on the url  /something and I want to use some 3-4 different responses to be returned for this call,  either sequentially or random. Like  send first response, then in next call, send second or third.  I want to test different size of response  and observe the application behavior. Please share if this is feasible and how.

Thanks,
Tina

aaron...@willowtreeapps.com

unread,
Mar 8, 2021, 1:45:33 PM3/8/21
to wiremock-user
If you wanted to have the responses in a certain order (response 1, response 2, etc.), you can use the scenarios functionality to achieve this. Check out the scenarios documentation.
```
{
    "scenarioName": "My Scenario",
    "requiredScenarioState": "Started",
    "newScenarioState": "Called Once",
    "request": {}
    "response": {
        "status": 200,
        "body": "First response"
    }
}
```

```
{
    "scenarioName": "My Scenario",
    "requiredScenarioState": "Called Once",
    "newScenarioState": "Called Twice",
    "request": {}
    "response": {
        "status": 200,
        "body": "Second response"
    }
}
```

If you always wanted them to be selected randomly, you could use the mustache templates to randomly pick a number and have that point to a response file.
```
{
    "request": {}
    "response": {
         "status": 200,
         "bodyFileName": "path/to/files/{{{pickRandom '1' '2' '3' '4'}}}.json"
}
```

You'd then want to have the four separate Json response files.

Tina B

unread,
Mar 8, 2021, 2:45:29 PM3/8/21
to wiremock-user
Yes. My requirement was not for the stateful one so I picked the second approach-  "bodyFileName": "path/to/files/{{{pickRandom '1' '2' '3' '4'}}}.json"  and it works very well. Thank You.

Tina 

Will Kuo

unread,
Aug 16, 2022, 9:02:37 AM8/16/22
to wiremock-user
Hi all,

     I have the same requirements (Random Responses for the same service call).
     For your provided approach (multiple response files, and pick random in "bodyFileName" setting).
     I am curious about:
         - Is the response file loaded in memory before calling? or is the file opened and read in runtime for every call?

     Furthermore, may I put all responses (maybe 100 responses) in a file, parse and load all responses in memory, then randomly pick one for the same service call?
         - Does Wiremock provide this approach?

     Thanks for the help.

Will
Reply all
Reply to author
Forward
0 new messages