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
Refcards Trend Reports
Events Video Library
Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
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

Integrating PostgreSQL Databases with ANF: Join this workshop to learn how to create a PostgreSQL server using Instaclustr’s managed service

Mobile Database Essentials: Assess data needs, storage requirements, and more when leveraging databases for cloud and edge applications.

Monitoring and Observability for LLMs: Datadog and Google Cloud discuss how to achieve optimal AI model performance.

Automated Testing: The latest on architecture, TDD, and the benefits of AI and low-code tools.

Related

  • Open-Source SPL That Can Execute SQL Without RDB
  • MongoDB to Couchbase: An Introduction to Developers and Experts
  • Looking for the Best Java Data Computation Layer Tool
  • Anatomy of a PostgreSQL Query Plan

Trending

  • Securing Your Applications With Spring Security
  • Cognitive AI: The Road To AI That Thinks Like a Human Being
  • How To Create a Resource Chart in JavaScript
  • Choreography Pattern: Optimizing Communication in Distributed Systems
  1. DZone
  2. Data Engineering
  3. Databases
  4. Writing Mocks With WireMock and CSV Extension

Writing Mocks With WireMock and CSV Extension

Learn about WireMock, and open-source solution to working with modules that have to communicate but that don't yet exist.

Nicolas Billard user avatar by
Nicolas Billard
·
Jul. 18, 17 · Opinion
Like (3)
Save
Tweet
Share
5.92K Views

Join the DZone community and get the full member experience.

Join For Free

If you are currently working on a project where several modules will have to communicate but they do not exist yet, then you may have to mock the communications.

And if you are using REST APIs, several tools might do the trick to provide a mocking server.

In the project I'm working on, we've chosen WireMock for several reasons — i.e. one standalone JAR, easy to integrate, easy to learn, etc.

The way we are using WireMock is based on a simple concept: writing JSON files containing some criteria to recognize a matching request and send the expected response directly.

If you want to learn more about WireMock, just read this article or this one. It's so simple that you have your first mock up and running in five minutes. 

The Problem

Unfortunately, this involves a major disadvantage: redundancy. Let's look at an example. Say you have a test dataset with five customers. You want to:

  • Find all customers (one file)

  • Retrieve a customer from its code (five files plus one for not found)

  • Validate a customer code: "true" if it exists; "false" otherwise (two files)

You will need nine JSON files. And if you want filters for the search, then you're lost. If you want to change the name of the customer, you have to change two files.

You should see what I mean.

For urgent tests, I have typed more than 100 JSON files with consistent data. And I thought never again.

The Solution

I searched for a solution to improve my day-to-day developing life with the following constraints:

  • Write data once and only once.

  • Adding data should be limited to adding data; no code.

  • It should be simple to add a new service mock using the existing data.

But found nothing in the existing WireMock extensions.

I thought about generating the result files from XML, CSV, etc. This would have been a bad idea. What if I add a new service allowing me to retrieve a customer from their mail? I would have to write new code for the generator. It would not be simple. And the filtering problem would still be unsolved.

You might be thinking, Dude, what you need is a database, and to code an app on top of it! Well...yes! Exactly! I need a database and SQL queries to retrieve data, and JSON responses generated directly and automatically from the SQL results. But I don't want to code an app each time I have to put up a mock server.

So the solution was to develop an extension to WireMock. This extension will perform the following:

  • Read data from CSV files with SQL as if it was a database but make it easier to deploy automatically with the mocks.

  • Extend the WireMock JSON files format to integrate the SQL in it. The SQL parameters will be the HTTP parameters.

  • The JSON responses will be generated automatically from SQL column names (or aliases).

Thus, the WireMockCSV extension was born. It has been enriched during the project with useful features, and now will handle:

  • Complex object hierarchy with objects lists, subobjects, sublists, etc. (Complex meaning with no limitation, but in general, it's not very complicated.)

  • Multiple SQL queries to build your response.

  • Some simple conditions.

  • Several options for personalization of the response.

Of course, there are no conflicts that WireMock can do normally. You can use all standard features in parallel even if you activate the extension.

Everything is documented on the extension's GitHub, with a full example to start with. All binaries are available on Maven Central.

It's open source, free to use, and free to distribute. Enjoy and save some time with it!

file IO Database sql

Opinions expressed by DZone contributors are their own.

Related

  • Open-Source SPL That Can Execute SQL Without RDB
  • MongoDB to Couchbase: An Introduction to Developers and Experts
  • Looking for the Best Java Data Computation Layer Tool
  • Anatomy of a PostgreSQL Query Plan

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • 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: