Saturday, February 23, 2019

HTTP Services Mocking with Wiremock-Implementation

     In previous blog, we have learnt the thearetical concepts of mocking services by wiremock. Also we have exercised few stubs using curl commands after setting up the wiremock locally. In this blog, lets learn the implementation of same stub services in java.

Post discussion of this blog you should be knowing how to handle the below implementation of 'http services life cycle by wiremock' in project.
The life cycle of stubbing(mocking) the wiremock services involves below processes:
Pre-requisites:
 Manually we need to have set up the running of wiremock service on apache tomact server. If you dont please refer previous blog.
 Initialisations:
    a. Start the wiremock server.
 Mock services and its usage:
    b. Stub the required services.
    c. Use the stubbed services on necessity.
 Deinitialisations:
    d. Unstub the services.
    e. Stop the wiremock server.

Initialisations:


Load the required mocking ip details using Properties class and configure the wiremock Notifier for verbose logging which helps in capture the stacktrace of logs to console. Start the wiremock server on configured ip and port.RestTemplate is the Sprint Rest Client for accessing the mocked services. You could use RestAssured, HttpClient if you are not comfortable with RestTemplate. Once the initialisations are done, we are good to stub the services. Next we see same stubbing and usage of services.
Note:
If you are running the standalone wiremock service locally on 8080 port then no need to configure server ip details using ConfigureFor() wiremock method. By default localhost:8080 will be pointed to when start() method is called.

Stub services and its usage:

GET-Stubbing

POST-Stubbing
The above images are self explanatory for stubbing the GET and POST http services.
The below images helps in understanding of how to use above static methods for stubbing using input http service data and usage of same stubbed services using RestTemplate spring Rest client.

Mock Get Service
Mock POST Service
   getUrlRequestMatch(), postUrlRequestMatch(),... are the static methods which accepts the required service parameters like url, headers, request body, response status, response body etc. that service to be stubbed.
Usage: Access the same stubbed http services using RestTemplate client and validate the response against expected stubbed data.

Deinitialisations:


Removes all the stubbed services and stops the running wiremock server. Trigger this method at the end of suite execution.


 This is all about the implementation of wiremock services in java and for more advanced concepts please refer wiremock official website: wiremock
P.S. You could get the same blog post with lot more examples and framework details from GitHub

















Featured post

Linux commands at work on day to day basis usage

  #To kill process by port: npx kill-port 3000 Or kill -9 $(sudo lsof -t -i:8080) #To run ssh file sh ./filename.sh   #To kill the pid by po...