Monday, May 30, 2022
HomeWordPress DevelopmentCreate Your First Mocked API Endpoint with WireMock

Create Your First Mocked API Endpoint with WireMock


WireMock is a software for mocking HTTP-based APIs that runs within the unit checks, on the desktop, or within the take a look at atmosphere. We are able to additionally say it’s a simulator for HTTP-based APIs, thought-about a service virtualization software or a mock server. It lets you keep productive when an API you rely upon Doesn’t exist and, Isn’t full or, Expensive to entry. It helps the testing of Edge circumstances and failure modes. It’s quick so reduces construct time considerably. In easy phrases, Wiremock is a mocking setup for integration testing. It’s primarily used through the improvement and extra considerably through the Integration testing whereas a system or service talks to at least one or a number of exterior or inside dependencies/companies.

Learn extra about WireMock on this article: Introduction to WireMock

On this article, we’re going to create our first mocked API endpoint with the assistance of WireMock and Postman.  

Step By Step Implementation 

Step 1:

Check with this text Obtain and Set up of WireMock and run the WireMock as a Standalone Course of in your native system. 

Step 2:

After efficiently operating the WireMock server let’s hit the next endpoint in your native browser and see what occurs. 

http://localhost:8080/testapi

So you’re going to get a message like this. Check with the under picture. 

No response might be served as there are not any stub mappings on this WireMock occasion.

 

You’re going to get the identical message within the WireMock console additionally. Check with the under picture. 

 

So what does it imply? This implies there are not any mocks being set. So let’s create this mock API “http://localhost:8080/testapi” after which we’ll hit this API once more. 

Step 3:

Open Postman and create a POST request. And put the next URL

http://localhost:8080/__admin/mappings

Word: There may be two underscore “_” earlier than the admin. Chances are you’ll conisde it like this “_ _admin”

So what we’re doing is making a CURL POST request to http://localhost:8080/__admin/mappings and that is the placement the place all of the mappings might be saved for the Wiremock server that we executed or began by way of the JAR file. 

Step 4: 

And within the Request Physique write JSON file as under

{
    "request": {
        "url": "/testapi",
        "methodology": "GET"
    },
    "response": {
        "standing": 200,
        "physique": "Welcome to GeeksforGeeks!"
    }
}

So right here we’re defining request parameters like, url and request methodology together with the response physique within the “response” part. This merely implies that at any time when a GET request is available in with URL /testapi then reply with the required response physique i.e “Welcome to GeeksforGeeks!” for this instance. And hit the Ship button. And within the Response you’re going to get one thing like this

{
    "id": "6197ad08-73d3-4eef-bd79-340c6f5c7145",
    "request": {
        "url": "/testapi",
        "methodology": "GET"
    },
    "response": {
        "standing": 200,
        "physique": "Welcome to GeeksforGeeks!"
    },
    "uuid": "6197ad08-73d3-4eef-bd79-340c6f5c7145"
}

Check with the under picture if you’re caught someplace. 

 

Step 5: 

Now lets hit the identical endpoint once more (http://localhost:8080/testapi) and see what occurs. 

 

Sure! this time we received our consequence. So we now have succfully created our first Mocked API Endpoint with the assistance of WireMock and Postman. Similary, you may mess around it an there’s many issues to discover in WireMock and its very very useful instruments for the Developer to carry out Integration and Unit testing. 

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments