Generate Random Data in Java using Data Faker (former Java Faker)

Last Updated:  July 17, 2022 | Published: July 20, 2019

Once our new prototype is ready for a first demo, we usually need some meaningful data to present it. Using foo, bar, duke, or other tech-related terms may not be the best option when showcasing software to a customer. This should be no problem if we already have a solid data source, but what about the opposite case? How to simply create meaningful random data for our Java application?

In the past, I've only used the UUID and ThreadLocalRandom  Java classes to create random data. As these are really limited and don't offer domain-specific random data, I searched for a better solution. While searching, I found the Faker gem for Ruby and, luckily, a Java port of it: Java Faker. With this easy-to-use library, we get access to many domains (e.g., books, food, persons, city, etc.) and can generate meaningful random data in seconds.

Update: As there's little maintenance activity in the Java Faker library, this article was updated to use the successor Data Faker.

Getting Started with Data Faker (former Java Faker)

We can include Data Faker (formerly Java Faker) in our Maven project with the following dependency:

To start using random data, we create an instance of the Faker class:

We can create a Faker instance once and don't have to instantiate an object whenever we need random data as the methods randomly access the underlying data. The library uses .yml files in /src/main/resources as its internal data store to retrieve random data.

With this library, we can access more than 30 different domains and create random data for nearly every use case. We can generate random data for a specific domain using the fluent API:

These domains include:

  • Finance
  • Food
  • Books
  • Name
  • Address
  • Business

and also funny domains like:

  • Yoda
  • ChuckNorris
  • HarryPotter
  • LordOfTheRings

To get a list of all available Fakers (domains), have a look at the README of the project.

Spring Boot Example with Data Faker (former Java Faker)

To demonstrate an example of this library in action, we're developing an API endpoint to return random persons, books, and food. We use Spring Boot 2.7.0 and Java 17, but any other Java setup would also work.

The sample application offers different endpoints to get random data:

Once we run the application, we can get a list of random persons at http://localhost:8080/random/persons:

Return Localized Random Data

The default locale for the random data is EN. In addition, the library offers the capability to return localized random data. If we specify a locale, we have to make sure it's supported by having a look at the corresponding .yml file to check if someone translated the English examples.

If there is no translation for our locale, e.g., beers, the library will fall back to English.

The locale is specified as a constructor argument of the Faker class:

This object instance is now capable of returning random data in German:

Or any other locale which is listed in the README of the project:

The source code for this example is available on GitHub.

If you're working with Kotlin, have a look at kotlin-faker.

Have fun creating random data with Java Faker,

Philip

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}
>