How to correctly create and configure a simulated 404 error

I have a GET request with the URL: {{url}}/provinces?id= (where I send the ID of the province I want to retrieve, only IDs 1 to 7 exist and are valid). I created 7 examples for this request, each with a different key-value pair (the key is the ID). If the value is 1, it returns the JSON object for the province with ID=1 and the status is 200 OK. The same applies for examples with ID=2, ID=3, …, ID=7.

Now, I need to create a simulation of a 404 error with a customized response that works when attempting to request any ID other than the existing ones. For example, if I send ID=0, 8, 345, 45lkhh, sldfgfliE, I want it to return a 404 with my custom response. I created a new example with a URL that includes a non-existent ID (ID=456) because I read somewhere that this would work to return a 404 for any request with an ID outside the range of 1 to 7. I changed its status to 404 Not Found, but it’s not working. In this case, it only returns a 404 if I request {{url}}/provinces?id=456 from the client. However, if I use 789, which also doesn’t exist, it applies the matching algorithm and returns a province that does exist with a 200 status code.

I appreciate your help in knowing how to configure a simulation of an error correctly. :smile:

Screenshot for the main request:

Screenshot about how the id=1 to id=7 are created:

Screenshot about how the “404 Not Found” customized example is created:

The mock isn’t that clever, it basically returns responses based on the request.

Which is why it works for the specific ID’s you have created 404 examples for.

There is some flexibility in there, but I don’t think its possible to return a 404 if it doesn’t match one of the ok responses.

I don’t think it will accept wildcards or regex patterns.

Purely from a testing perspective, it sounds like you are trying to do a boundary test. Therefore, you only really need enough 404 examples to cover those boundaries. People shouldn’t be sending data outside of those parameters to the mock. You should be in control of your test data, and it includes these 404 tests.

PS: Your requests don’t appear to be using a mock. We are talking about the Postman Mocking features right?

¡Hello Mike Jones, thanks for your response!

This is my first interaction with Postman Mocking features.

Correct, I have a mock server linked to this collection.
You’re suggesting that from a testing perspective, I should simply add controlled error cases and return them as a 404, right?

Is there any other way to do it differently from mine, so that the return of specific responses based on ID becomes more dynamic and includes customized 404 responses by default without specific error cases? I’m not sure if my idea contradicts the concept of “mocking”.

Yes, as a QA Lead, I always advise to add controlled test cases with known test data, and that is for happy path and negative testing.

Sometimes generating errors is easier said than done.

I don’t use the mocking features in Postman that much. There are some options in relation to returning dynamic data but I don’t think this will cover what you are trying to achieve with the status code.

Hopefully someone with more experience of the mocking service will be able to provide some insight.

The following link has more details on the matching algorithm.

How a Postman mock server matches requests to saved examples | Postman Learning Center

I did have a quick look through the documentation to remind me what is possible.

You might want to look at the section on related to headers.

x-mock-response-name or x-mock-response-id enable you to further specify the example to be returned based on the name or the UID of the saved example.

It might be possible inject this header into the request using the pre-request script. The script needs to pull back the value of the ID query parameter and if it should produce a 404 not found, get the script to add the header.

Thank you for sharing your experience and advice from your QA background; I will certainly take them into account!

I will delve deeper into the options you mentioned and try to find the best way to approach this situation

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.