Skip to content

SkillPanel/jfairy

jFairy by Devskiller

Build Maven Central Javadocs

Java fake data generator. Based on Wikipedia:

Fairyland, in folklore, is the fabulous land or abode of fairies or fays.

Installation

<dependency>
    <groupId>com.devskiller</groupId>
    <artifactId>jfairy</artifactId>
    <version>0.8.1</version>
</dependency>

Usage

Creating simple objects:

Fairy fairy = Fairy.create();
Person person = fairy.person();

System.out.println(person.getFirstName());
// Chloe
System.out.println(person.getLastName());
// Barker
System.out.println(person.getEmail());
// barker@yahoo.com
System.out.println(person.getTelephoneNumber());
// 690-950-802
System.out.println(person.getJobTitle());
// Software Developer

Person adultMale = fairy.person(PersonProperties.male(), PersonProperties.minAge(21));
System.out.println(adultMale.isMale());
// true
System.out.println(adultMale.getDateOfBirth());
// at least 21 years earlier

Creating related objects:

Fairy fairy = Fairy.create();
Company company = fairy.company();
System.out.println(company.getName());
// Robuten Associates
System.out.println(company.getUrl());
// http://www.robuteniaassociates.com

Person salesman = fairy.person(PersonProperties.withCompany(company));
System.out.println(salesman.getFullName());
// Juan Camacho
System.out.println(salesman.getCompanyEmail());
// juan.camacho@robuteniaassociates.com

Supported locales

Locale Language tag
English (default) en
Polish pl
German de
French fr
Spanish es
Swedish sv
Chinese zh
Georgian ka
Italian it
Brazilian Portuguese br
Slovak sk
Fairy enFairy = Fairy.create();
// Locale.ENGLISH is default
Fairy plFairy = Fairy.create(Locale.forLanguageTag("pl"));
// Polish version
Fairy brFairy = Fairy.create(Locale.forLanguageTag("br"));
// Brazilian version

Unique values

UniqueFairy unique = fairy.unique();
Person p1 = unique.person();  // unique by email
Person p2 = unique.person();  // different email than p1
Company c = unique.company(); // unique by name

For custom uniqueness keys:

UniqueEnforcer<Person> unique = UniqueEnforcer.of(fairy::person, Person::getFullName);
Person p = unique.next();

Thread safety

Fairy object should not be used concurrently by multiple threads. It is recommended to create Fairy instance for each thread.

Some methods are not thread-safe when multiple threads share the same Fairy object. For other methods it is still recommended to create a separate Fairy object, because Random object utilized underneath does not perform well when used concurrently by multiple threads.

Other samples

Look into code samples

JUnit 5 Extension

If you use JUnit 5, check out jfairy-junit-extension — a JUnit 5 extension that allows injecting jFairy-generated objects directly into test method parameters.

Building

This project can be built using maven command:

./mvnw install

Packages

 
 
 

Contributors