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
Refcards
Trend Reports

Events

View Events Video Library

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

  • Lambda-Driven API Design: Building Composable Node.js Endpoints With Functional Primitives
  • Run Gemma 4 on Your Laptop: A Hands-On Guide to Google's Latest Open Multimodal LLM
  • Bringing Intelligence Closer to the Source: Why Real-Time Processing is the Heart of Edge AI
  • Feature Flag Debt: Performance Impact in Enterprise Applications
  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.

By 
Nicolas Billard user avatar
Nicolas Billard
·
Jul. 18, 17 · Opinion
Likes (3)
Comment
Save
Tweet
Share
6.7K 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

Partner Resources

×

Comments

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

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook