Skip to content
Advertisement

How to set up different responses for the same request to MockServer?

i’m having an issue when setting up the MockServerClient for multiple responses with the exact same request.

I read that with expectations with “Times” this might be done, but i coulnd’t make it work with my scenario.

If you call the service with this JSON (twice):

JavaScript

The first response should be “passed true”, the second “passed false”

Response 1:

JavaScript

Response 2:

JavaScript

I set up the first request, but how do i set the second one?

JavaScript

Dependencies (relevant):

JavaScript

Thank you in advance.

Advertisement

Answer

After following and diving into the documentation and testing.

I found that you can specify a “Times” that an expectation will be match which solves perfectly my problem.

Link: https://www.mock-server.com/mock_server/creating_expectations.html

For every expectation i used “Times.exactly(1)“.

The way this works is that an expectation is added to the list, when it’s matched it will be consumed, and removed from the list, leaving the following ones.

If no expectation is found for a call it will return a 404 from the mock server.

Link for examples from documentation: https://www.mock-server.com/mock_server/creating_expectations.html#button_match_request_by_path_exactly_twice

Correct code:

JavaScript
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement