Mock REST API calls with Mockoon

Mock REST API calls with Mockoon

Did you ever wish you could mock REST API calls?

This post will quickly introduce Mockoon, a free open source solution, to maybe help others in the righteous path of software design and test automation!

Have you already been blocked in your test automation by HTTP calls? Are you interested in developing a frontend application even if the backend is not available?

This is a limitation I often come across but I never found the right way to do it, until now!

Mockoon

As mentionned in Mockoon’s website:

Mockoon is the easiest and quickest way to design and run mock REST APIs. No remote deployment, no account required, free and open-source.

The solution delivers the following artifacts:
– a Graphical User Interface (GUI): mandatory to edit the data json file (defining the REST API mocks)
– a command line tool called mockoon-cli: easy to install and run in a pipeline
– an official Docker image (generated by this Dockerfile): to execute the CLI in a NPM-free system

Key concepts

1/ All the configuration of one Mockoon “environment” is stored in a json file (ideally versioned in git)

2/ Mockoon uses a templating engine (in particular Faker library) to be able to generate random data and use loops

3/ There are samples available on many systems and it’s possible to import the definition from a Swagger/OpenAPI file

Screenshot of Mockoon GUI

Quick start

Here is a short extract with the minimal commands to test the tool.

# installs the command line interface (assuming NPM is installed)
npm install -g @mockoon/cli

# starts the mocking server by using a sample data file
mockoon-cli start --data https://raw.githubusercontent.com/mockoon/mock-samples/main/samples/generate-mock-data.json --port 3000

# lists running mockoon instances
mockoon list

# makes a call to the mock api
curl http://localhost:3000/posts

# deletes the running mockoon instances
mockoon stop all

From there, you can “create your first mock API with Mockoon” by following the official Getting Started page.

Opening

You can see Mockoon in action in the Continuous Integration (CI) pipeline of rabbids-incubator/servicenow-dotnet-client, an open source project that is making calls to ServiceNow REST API (see gitlab-ci.yml).

That’s all for now, happy mocking and please share your feedback and ask any question you may have in the comment section!

bertrand
Latest posts by bertrand (see all)

Leave a Reply

Your email address will not be published. Required fields are marked *