Faking Data

It's showcase time, your application has been deployed and the product owner comes around the corner. The login screen loads up but you realise there's no data in the system. Valuable seconds tick by and yet you can't think of any creative customer names besides from "John Smith" and "Jane Doe."

Enter the Java Faker library. This is a port of the popular Ruby Faker gem. The idea behind the library is very simple. It generates fake data. The library actually came out of a software project developed by my company DiUS Computing. We used it to generate data for showcases. The rest of this article will show how this library is used.

1.The first thing you need to do is add the dependency to your build tool. This example uses maven.

<dependency>  
    <groupId>com.github.javafaker</groupId>
    <artifactId>javafaker</artifactId>
    <version>0.5</version>
</dependency>  

2.With the dependency resolved, you can begin to use the library.

Start by instaniating an instance of the Faker.

Faker faker = new Faker();  

3.Start creating fake data now. It is as simple as that.

String name = faker.name().fullName();  
String streetAddress = faker.address().streetAddress();  
String emailAddress = faker.internet().emailAddress();  

Look at the javadocs for examples of what fake data can be generated.

I'm the current maintainer of the library, so if you have any ideas on how to make the library better or any proposed changes, let me know or send me a pull request. Happy Faking!

References