DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Testing REST Controller Methods With JUnit 5 [Video]
  • Update User Details in API Test Client Using REST Assured [Video]

Trending

  • Tired of Spring Overhead? Try Dropwizard for Your Next Java Microservice
  • How To Build Resilient Microservices Using Circuit Breakers and Retries: A Developer’s Guide To Surviving
  • Vibe Coding With GitHub Copilot: Optimizing API Performance in Fintech Microservices
  • Developers Beware: Slopsquatting and Vibe Coding Can Increase Risk of AI-Powered Attacks
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. WireMock: Mock Your REST APIs

WireMock: Mock Your REST APIs

Check out WireMock, and learn about developing and identifying REST APIs to show response and request information.

By 
Siva Prasad Rao Janapati user avatar
Siva Prasad Rao Janapati
·
Mar. 04, 16 · Analysis
Likes (25)
Comment
Save
Tweet
Share
81.4K Views

Join the DZone community and get the full member experience.

Join For Free

As we are extensively working on “OmniChannel” applications with REST APIs approach, mocking the REST services is essential in our day to day development. As part of our development first we are going to identify the REST APIs to expose and the request and response details. Once we finalize these details the backend development team use to create sample request/response (i.e XML/JSON response) for each API and the same can be consumed by the front end team and the native app development team. Till the backend development team provides the APIs the other teams use to hardcode the sample request and responses to complete the development. The “WireMock” provides an elegant way to mock the REST APIs. The below diagram depicts how “WireMock” fits in our development environment.

WireMock

                                 








We can run WireMock as “Standalone mode” or “deployed into servlet container.” In this article, we will see running WireMock as standalone mode. To setup, WireMock follow the below steps.

Step 1: Download the WireMock standalone jar.

Step 2: Run the standalone jar by giving below command.

java -jar wiremock-1.57-standalone.jar --verbose

There are several command line options available. You can find the same here. If you are not passing –port option by passing the port number, by default it will run on 8080.

After running the WireMock, you will see the below folder structure from where the WireMock standalone app is running.

WireMock_Folder_Structure




Now, if you send the request to http://localhost:8080/_admin, then you will empty mappings.

WireMock Mappings





Step 3: Now, we will create mapping files for GET and POST requests. Here I want to simulate GET product and CREATE product requests. Now we will see GET request and response mappings.

WireMock_GET_Request













Create product.json file under “mappings” folder depicted in step2. Make sure that the body content is properly formatted by keeping the content in a single line and the double quotes are escaped.


{
 "request":
 {
 "urlPattern": "/product/p0001",
 "method": "GET"
 },

 "response":
 {
 "status": 200,
 "headers":
 {
 "Content-Type" : "application/json"
 },
 "body": "{ \"_id\" : \"p0001\", \"product_display_name\" : \"Brother - MFC-J4320DW Wireless All-In-One Printer - Black/Gray\", \"category_ids\" : [ \"pcmcat247400050001\" ], \"is_active\" : true, \"on_sale\" : true, \"price\" : { \"list_price\" : 149.99, \"sale_price\" : 123.99 }, \"upc\" : \"012502637677\", \"mpn\" : \"MFC-J4320DW\", \"manufacturer\" : \"Brother\", \"product_short_description\" : \"4-in-1 functionalityBuilt-in wireless LAN (802.11b/g/n)Prints up to 20 ISO ppm in black, up to 18 ISO ppm in color (Print speeds vary with use. See mfg. for info on print speeds.)150-sheet tray2.7\\\"\" }"
 }
}

Step 4: Now, restart the WireMock and send the request to http://localhost:8080/product/p0001. You will get the product details JSON response.

WireMock GET Response






Step 5: We will see how to create POST request and response mapping.

WireMock POST Request










Place the mapping JSON file under “mappings” folder and restart the server.

The mapping JSON is given below.


{
 "request":
 {
 "urlPattern": "/createProduct",
 "method": "POST",
 "bodyPatterns" : [{
 "equalToJson" : "{ \"product_display_name\" : \"Brother - MFC-J4320DW Wireless All-In-One Printer - Black/Gray - New Product\", \"category_ids\" : [ \"pcmcat247400050001\" ], \"is_active\" : true, \"on_sale\" : true, \"price\" : { \"list_price\" : 149.99, \"sale_price\" : 123.99 }, \"upc\" : \"012502637677\", \"mpn\" : \"MFC-J4320DW\", \"manufacturer\" : \"Brother\", \"product_short_description\" : \"4-in-1 functionalityBuilt-in wireless LAN (802.11b/g/n)Prints up to 20 ISO ppm in black, up to 18 ISO ppm in color (Print speeds vary with use. See mfg. for info on print speeds.)150-sheet tray2.7\\\"\" }"
 }]
 },

 "response":
 {
 "status": 200,
 "headers":
 {
 "Content-Type" : "application/json"
 },
 "body": "{\"_id\" : \"p0002\", \"product_display_name\" : \"Brother - MFC-J4320DW Wireless All-In-One Printer - Black/Gray - New Product\", \"category_ids\" : [ \"pcmcat247400050001\" ], \"is_active\" : true, \"on_sale\" : true, \"price\" : { \"list_price\" : 149.99, \"sale_price\" : 123.99 }, \"upc\" : \"012502637677\", \"mpn\" : \"MFC-J4320DW\", \"manufacturer\" : \"Brother\", \"product_short_description\" : \"4-in-1 functionalityBuilt-in wireless LAN (802.11b/g/n)Prints up to 20 ISO ppm in black, up to 18 ISO ppm in color (Print speeds vary with use. See mfg. for info on print speeds.)150-sheet tray2.7\\\"\" }"
 }
}

Step 6: Send the POST request by using any request poster. Here I used “POSTMAN” and send the request by passing request body.

WireMock POST Response

By following above steps you are equipped to mock the services, and WireMock is handy to that. There are similar alternative tools to try.

  • Betamax
  • REST-driver
  • MockServer
  • Moco
REST Web Protocols

Published at DZone with permission of Siva Prasad Rao Janapati, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Building a REST Service That Collects HTML Form Data Using Netbeans, Jersey, Apache Tomcat, and Java
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Testing REST Controller Methods With JUnit 5 [Video]
  • Update User Details in API Test Client Using REST Assured [Video]

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!