Skip to main content

Generating Test Data – jFairy

Test data has been always an issue. If you are running selenium or backend automated tests based on user-related scenarios, in order to make your tests more efficient you need to provide unique and realistic user test data. There’re many ways to deal with this: dumping samples of production databases, writing your own data generators or using the very same data in every test run and cleaning database afterwards.

In this post I want to write about small and handy Java library for generating fake test data – jFairy. Since the library is super-simple to use, this post is just the shout-out for the nice tool I’ve been using in many different automation projects and I hope I’ll put a spotlight on it for my readers.


If you use maven, to include jFairy in your project add the following dependency:


For gradle:

compile group: 'io.codearte.jfairy', name: 'jfairy', version: '0.5.9'

Use examples 

First thing to do is to call static method create() to create Fairy class instance:

Fairy fairy = Fairy.create();

From now on you can generate test data. Let’s start with creating random person:

This prints random person’s data, like one following:

Gavin Rodgers 
31 Summer Place 
New York 23795

As you can see email data matches person’s first and last name. You can also specify nationality of generated person’s data. The moment I’m writing this post you can use locale: en, de, es, ka, pl, sv and zh. Try pass locale as follows:

Fairy fairy = Fairy.create(Locale.forLanguageTag("de"))

This will print analogous test data to previous one, but for german citizen:

Zacharias Wohlgemuth 
Herrenkrugstraße 169, 151 
18421 Brannenburg

The feature I like the most about jFairy is the ability to specify whether you want to generate male or female person’s data. In the below example we’re generating test person who is female with age between 20 and 30. This time I created simple Spock test to prove it:

This time jFairy gave us Camila:

Camila Melendez 

…and the test passes, so isFemale() method returned true.

You can go further with using jFairy than creating fake personal data. You can create entities such as company’s data:

Which gives us the brilliant startup idea:

MemorTech Limited 

Or you can generate IP numbers:

Which can give you IP number like one below:

The goal of this post isn’t to describe all of jFairy features, but to bring your attention to the project. Since the library is quite small I encourage you to explore its implementation for your own – it has more to offer. Are there any cons? I wouldn’t call it con, but generating credit card numbers is challenging and repetitive task (control number) and Fairy.creditCard() gives us only cardVendor and expiryDate. Well, sounds like a pull request idea!

Popular posts from this blog

REST-Assured framework overview

In modern software development, REST services becomes most popular choice for implementing distributed and scalable web application. They are light and easy to maintain, which results in faster and more effective implementation and system integration.
I recommend you also my other posts about REST-Assured and building microservice’s test automation frameworks: REST-Assured: going deeperBuilding microservices testing framework
With the increase popularity of RESTful services, there is a need for fast and lightweight tool for REST webservices testing automation. One of the most popular choice is Rest-Assured framework from Jayway. It introduces simplicity of testing web services from dynamic languages like groovy or ruby to java. In this post we will get our hands dirty and write automatic test in Rest-Assured framework.
In order to create complete implementation of automated tests in Rest-Assured framework, we need to write our code against some example API. We’ll use standalone Wiremock m…

Testing Asynchronous APIs: Awaitility tutorial

Despite the growing popularity of test automation, most of it is still likely to be done on the frontend side of application. While GUI is a single layer that puts all the pieces together, focusing your automation efforts on the backend side requires dealing with distributed calls, concurrency, handling their diversity and integration.
Backend test automation is especially popular in the microservices architecture, with testing REST API’s. I’ve noticed that dealing with asynchronous events is particularly considered as challenging. In this article I want to cover basic usage of Awaitility – simple java library for testing asynchronous events. All the code examples are written in groovy and our REST client is Rest-Assured.
Synchronous vs Asynchronous In simple words, synchronous communication is when the API calls are dependent and their order matters, while asynchronous communication is when the API calls are independent. Quoting Apigee definition:
Synchronous  If an API call is synchrono…

Notes after TestingCup 2018

On May 28-29th I attended TestingCup conference in Łódź. Having quite unique perspective: this was my second year in row as a Speaker at this conference I want to share some thoughts on the event. Dust has settled, lets go! 

Championship Originally TestingCup is a software testing championship. Wait, what? Yes, the formula is unique: teams and individuals from all around Poland are competing in finding the most bugs and defects in specially prepared application - Mr. Buggy. I don’t have specific data, but since this year’s conference was all english I guess competitors were not only from Poland. As a spectator, I must say that the whole competition looked very professional. There were team shirts and names, podium and trophies (gold cup and cash). 
Some cons? Testing championship is held at the first day of the conference. So this is already a conference, but if you’re not taking part in the championship… there’s not much to do, since all the talks are in the next day. Organizers are aw…