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

Test Automation: Good, Bad and Ugly

The modern approach to software quality and software development life cycle requires that business guys, developers and testers understand that the long manual test phase, although often still necessary, must be reduced to a minimum and replaced by test automation. Working in continuous delivery and continuous integration environment requires us to create automated tests that run on demand, checking our application integration and it’s core functionality correctness. However, there are still many problems with designing and writing automated tests, resulting in their costly maintenance or abandonment in favour of a return to manual processes.
In this article I will focus on describing common good practices of test automation. This post is more than an overview than complete reference guide. Broader aspects, such as the Page Object pattern or Page Factory will be described in detail in a separate article on this blog. Although most practices apply for every types of automated tests, thi…

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…

REST-Assured: going deeper

In my previous post I described the basic REST-Assured usage – the lightweight framework for testing RESTful services. Despite the fact that described range of functionalities would be enough in most cases,REST-Assured has a lot more to offer. In this post I would like to bring some more advanced examples of the framework usage.

I recommend you also my other posts about REST-Assured and building microservice’s test automation frameworks:

REST-Assured – framework overviewBuilding microservices testing framework
Object Mapping Sending request’s body as string is easy and straightforward, but it can be inconvenient in the case of more complex operations on request / response properties. Proven solution for this is a good-known serialization of request/response body to objects. REST-Assured supports object mapping to (and from) JSON and XML. For JSON you need either to have Jackson or Gson in your classpath and for XML you need JAXB. Here is an example of request object serialization using J…