For same endpoint returning different responses

781 views
Skip to first unread message

Rishav Srestha

unread,
Dec 29, 2022, 12:22:03 AM12/29/22
to wiremock-user
Greetings,

We are writing integration tests wherein we have multiple tests around a single endpoint.
Now we need to return different responses for each of these tests for that same endpoint.
Eg-
Test1 :
request URL: /some/path [GET]
response : 200 

Test2 :
request URL: /some/path [GET]
response: 400

Test3:
request URL: /some/path [GET]
response: 500

Note that these are multiple individual tests in a single test file. 

Question:
We were wondering if it is possible to use the JSON mappings approach for such a scenario. That is, could we do something like:
-------------------------------------------------------------------
*wiremockMappings.json*
-------------------------------------------------------------------
{
          {
                "test" : "Test1",
                "request": {
                                   "method": "GET",
                                    "urlPath": "/some/path" 
                                    },
                 "response": {
                                    "status": 200,
                                   }
             },
              {
                 "test" : "Test2",
                "request": {
                                   "method": "GET",
                                    "urlPath": "/some/path" 
                                    },
                 "response": {
                                    "status": 400,
                                   }
             },
            {
                 "test" : "Test3",
                "request": {
                                   "method": "GET",
                                    "urlPath": "/some/path" 
                                    },
                 "response": {
                                    "status": 500,
                                   }
             }
}
-----------------------------------------------------------------------------------------
Any suggestions as to deal with such a scenario is most welcomed.
Note: we are using wiremock 2.35.0 with Java's Spring f/w. 

Thank you,
Rishav            

aaron...@willowtreeapps.com

unread,
Dec 29, 2022, 10:00:05 AM12/29/22
to wiremock-user
If you wanted to use the mappings file, you could use Scenarios to achieve this.

{
  "mappings": [
    {
      "scenarioName": "Foo",
      "requiredScenarioState": "Started",
      "newScenarioState": "Test 1 Complete",
      "request": { ... },
      "response": { ... }
    },
    {
      "scenarioName": "Foo",
      "requiredScenarioState": "Test 1 Complete",
      "newScenarioState": "Test 2 Complete",
      "request": { ... },
      "response": { ... }
    },
    ...
  ]
}

Things to note: 
  • Your requests would need to have the same "scenarioName". 
  • All scenarios, on WireMock starting up, begin in a state of "Started", which is why the first mapping has that "requiredScenarioState".
  • At the conclusion of your last test, if you wanted to cycle through again, you'd need to set the "newScenarioState" to "Started".
  • Using this requires you to have your tests always execute in a certain order. Otherwise, you'd match to the incorrect scenario state.
If you're using Spring and starting/controlling WireMock via Java, you can add/remove stubs in the tests.

Rishav Srestha

unread,
Jan 2, 2023, 4:37:37 AM1/2/23
to wiremock-user
Hello ,

Thanks for the quick response.
There is, however, a catch with the above approach which I observed, please correct me if I'm wrong-

It seems the scope of changing/chaining the scenarios is limited within the test itself.
I.e. Each test is always matching with the "Started" scenario first and always returning the first response. 

So, unless there is a call to that endpoint in that same test, ie. if the same test hits the endpoint 3 times only then the different scenarios are picked up.

Hence, I'm not sure if scenarios could be useful for this use case. 
Please correct me if I'm wrong.

-------------------------------------------------------------
Here is the mappings file I was using-- 
{
"mappings": [
{
"scenarioName": "API1",
"requiredScenarioState": "Started",
"newScenarioState": "test1done",
"request": {"method": "PUT","urlPath": "/v1/endpoint"},
"response": {status": 200},
{
"scenarioName": "API1",
"requiredScenarioState": "test1done",
"newScenarioState": "test2done",
"request": {"method": "PUT","urlPath": "/v1/endpoint"},
"response": {"status": 400},
{
"scenarioName": "API1",
"requiredScenarioState": "test2done",
"newScenarioState": "test3done",
"request": {"method": "PUT","urlPath": "/v1/endpoint"},
"response": {"status": 500}
}
]
}

And I have 3 Different tests, each of which are hitting the same endpoint but expecting different response for their specific testcase.


Thanks
Rishav
Reply all
Reply to author
Forward
0 new messages