Sunday 31 May 2020

Run WireMock in standalone mode

Step 1: Go to following location, navigate to the latest version of wiremock and download latest jat file.

https://repo1.maven.org/maven2/com/github/tomakehurst/wiremock-standalone/

 

At the time of writing this article 2.9.0 is the latest wiremock version.

$ls
wiremock-standalone-2.9.0.jar


Run wiremock in standalone mode by executing the below command.

java -jar wiremock-standalone-2.9.0.jar

$java -jar wiremock-standalone-2.9.0.jar
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
 /$$      /$$ /$$                     /$$      /$$                     /$$      
| $$  /$ | $$|__/                    | $$$    /$$$                    | $$      
| $$ /$$$| $$ /$$  /$$$$$$   /$$$$$$ | $$$$  /$$$$  /$$$$$$   /$$$$$$$| $$   /$$
| $$/$$ $$ $$| $$ /$$__  $$ /$$__  $$| $$ $$/$$ $$ /$$__  $$ /$$_____/| $$  /$$/
| $$$$_  $$$$| $$| $$  \__/| $$$$$$$$| $$  $$$| $$| $$  \ $$| $$      | $$$$$$/ 
| $$$/ \  $$$| $$| $$      | $$_____/| $$\  $ | $$| $$  | $$| $$      | $$_  $$ 
| $$/   \  $$| $$| $$      |  $$$$$$$| $$ \/  | $$|  $$$$$$/|  $$$$$$$| $$ \  $$
|__/     \__/|__/|__/       \_______/|__/     |__/ \______/  \_______/|__/  \__/

port:                         8080
enable-browser-proxying:      false
no-request-journal:           false
verbose:                      false

As you the console output, you can confirm that wiremock stared and listening on port 8080.

 

Open the url 'http://localhost:8080/__admin/recorder/' to see recorder user interface.


Once you start WireMock in standalone mode, you can see that following two folders are created.

 

a.   mappings : It contain request, response json

b.   __files: It contains response messages, errors json

 

$ls
__files       mappings      wiremock-standalone-2.9.0.jar

 

Let’s create an end point /employees/1

Create employeesById.json file in mappings folder.

$tree
.
├── __files
├── mappings
│   └── employeesById.json
└── wiremock-standalone-2.9.0.jar

2 directories, 2 files

employeesById.json

{
  "request": {
    "urlPattern": "/employees/1",
    "method": "GET"
  },
  "response": {
    "status": 200,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "{ \"id\":1,\"firstName\" : \"Rama\",\"lastName\": \"Gurram\"}"
  }
}

Restart WireMock application.

 

Open the url ‘http://localhost:8080/employees/1’ in browser, you will see following json.

{ "id":1,"firstName" : "Rama","lastName": "Gurram"}


Create an employee

Create ‘createEmployee.json’ file in mappings folder.

 

createEmployee.json

{
  "request": {
    "urlPattern": "/employees",
    "method": "POST",
    "bodyPatterns": [{
      "equalToJson": "{\"firstName\":\"Ram\",\"lastName\" : \"Gurram\"}"
    }]
  },
  "response": {
    "status": 201,
    "headers": {
      "Content-Type": "application/json"
    },
    "body": "{\"id\":1,\"firstName\":\"Ram\",\"lastName\" : \"Gurram\"}"
  }
}

Restart WireMock Service.

 

Open any REST client like postman and hit following request.

 
API: http://localhost:8080/employees
Method: POST
Payload:
{"firstName":"Ram","lastName" : "Gurram"}
 

You will get following response.

{
    "id": 1,
    "firstName": "Ram",
    "lastName": "Gurram"
}





Previous                                                    Next                                                    Home

No comments:

Post a Comment