Mock APIs Using sMockin
This tutorial aims to explain how to improve time when it is required for third-party system integration. Read below to find out how to do this yourself!
Join the DZone community and get the full member experience.Join For Free
The main goal of this article will be to explain how to improve our time when is required to integrate with a third-party system. Usually, it is an API and the developer doesn't have control over it. Moreover, the developer usually needs to mock the API, the sMockin could help the teams with this hard task.
What Is a Mock API Server?
A mock API imitates a real API server by providing realistic mock API responses to requests. They can be on your local machine or the public Internet. Responses can be static or dynamic, and simulate the data the real API would return, matching the schema with data types, objects, and arrays.
A Simple Overview About sMockin:
sMockin is an API mocking tool used to dynamically simulate HTTP endpoints.
Whether you are a developer who needs to simulate RESTFul endpoints or an engineer working with complex SOA / IoT / microservice infrastructure, sMockin can help by mimicking any services that are either unavailable or otherwise too difficult or time-consuming to set up.
sMockin runs as a small web app that can be either installed locally onto a personal machine or hosted centrally and used by multiple users, all information over sMockin is here.
Starting and Creating a Mock API
- Java 8 and Maven
Mocking an API
- Download the code and run the sMockin.
git clone https://github.com/mgtechsoftware/smockin.git
- Run the mvn install
$ cd smockin $ mvn clean install
- Install and Start
$ ./install.sh $ ./start.sh
Start the Mock Server
Open this URL.
Create an HTTP mock:
- Click on the New HTTP Endpoint button.
- Create the HTTP mock as you desire.
That's it! After you test the mocks API, call the endpoint using curl or other tools, then you'll see the test was done using the hey(hey -n 22 -c 1 -m GET http://localhost:8001/invoices). In the image below, there are 10 requests with status code 200, 10 with status code 400 and 2 with status code 500.
And, we're finally done. I hope this article sheds some light on how to use Smockin to mock API responses for requests. sMockin has other options; it can be used as standalone, binary, source, and docker.
Opinions expressed by DZone contributors are their own.