Using Mockoon to Speed Up API Mocking and Testing

COMMENTS (0)
Tweet

With the increased demand for rapid software development, API mocking and testing can be one of the most time-consuming tasks in application delivery. However, creating the necessary API mocking environment can often be challenging and time-consuming.

This is where tools like Mockoon offer potent means for developers to speed up API mocking and testing. Mockoon is a simple tool for creating and sending mock APIs to test applications.

It is an open-source tool that helps us create and mimic real APIs. In minutes, you can set up a mock server and routes! (You can download Mockoon from here.) It also helps developers create mock servers for their APIs and test different scenarios for their applications without using an external service or dependency.

However, let’s start with the basics and delve more deeply.

How to Use Mockoon to Speed Up API Mocking and Testing?

Here’s how you can use Mockoon to speed up API mocking and testing:

Setup a Basic Mock Server

To set up a primary mock server, you should:

  1. Make a new environment 
  2. Add a route
  3. Then add some sample data in either the Inline editor or the Data Bucket for the route to fetch
  4. Now select this data bucket for the ‘/models’ route under ‘Status & Body.’
  5. Then, start the server and hit the request by:
  6. Clicking on server settings to get the full URL, set your port and prefix (if needed)
  7. Start the server and request Postman.
  8. Postman Result
  9. Congratulation! We just made our mock server and a mock API!

How Can We Build Up Further on This Implementation?

Now, let’s build up further on this implementation.

1. Add Crud APIs

You can add a CRUD route of your own. GET routes in Mockoon simply fetch all the items in the list in the data bucket, but Mockoon provides functionality in GET routes under the CRUD route to fetch the items by ID, for instance, localhost:3000/API/v1/:id

2. Add Rules and Multiple Responses to the same route

Mockoon provides a functionality to add rules to your routes. We can add query params route params to our routes to filter the data based on our use case. For example, a ‘/training’ CRUD route can be used with all the training data in the data bucket.

 

If you want to fetch different data from a single route, you can add a new response and rule to that route and attach a different data bucket.

This means we added a new response (Response 1 (200)) to the ‘/training’ CRUD route and a rule where we add a query string type=history to the URL. The URL under this response and the route looks like this: localhost:3000/API/v1/trainings?type=history. Then, attach a different data bucket to Response 1 (Here, Trainings Metadata – History of deployments and training).

Now, we can say we have 

  1. localhost:3000/API/v1/trainings
  2. localhost:3000/API/v1/trainings?type=history

You can add as many rules and responses as you want.

3. Use Dynamic data in the data bucket

Instead of manually writing up all the mock data in the data buckets, you can speed up the process using faker.js helper templates in Mockoon.

Here, I have written one template and repeated it nine times to get multiple rows with different data. Mockoon uses handlebar syntax for templating.

4. Then Use Data from another data bucket

You can use data from another data bucket.

In this template, I use each row from the Data Bucket ‘Training list and reuse each item’s ID and other attributes.

5. Lastly, share your environment

Now that you have added multiple functions to your server, you can share this with your peers as-is or add it to your project in the repo so that it remains maintained.

Locate your environment file and then share it.

Conclusion

API mocking and testing can be a bottleneck in a world where rapid software development is paramount. However, tools like Mockoon offer a solution, simplifying the process with its user-friendly interface. By following these steps and leveraging its features, developers can significantly accelerate API mocking and testing, enhancing workflow efficiency.

CALL

USA408 365 4638

VISIT

1301 Shoreway Road, Suite 160,

Belmont, CA 94002

Contact us

Whether you are a large enterprise looking to augment your teams with experts resources or an SME looking to scale your business or a startup looking to build something.
We are your digital growth partner.

Tel: +1 408 365 4638
Support: +1 (408) 512 1812