Generate Random Data using Java Faker

Thameem Ansari
2 min readDec 29, 2021

--

Java Faker

Java Faker is a library used to generate fake data. It provides different classes and methods in order to generate real-looking data that ranges from mobile number, address, music, nation and many more. This is really helpful when we want to use some placeholder but don’t have actual data. For example, you want to generate debit card data and want to achieve validation against it. This is possible using Faker library.

It also provides you access to more than 35 different domains and provides data nearly to every real-life use case. The domains like below

  • Finance
  • Food
  • Books
  • Name
  • Address
  • Business
  • HarryPotter
  • LordOfTheRings

Please check all the list of available domain and locale it supports:

In this article, we will be looking at how to generate dummy data with the help of JavaFaker’s classes.

Dependencies

Below is the single dependency we will need for maven based projects.

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

Faker Class

Let us see how to instantiate a Faker object and use it to call some fake data.

Faker faker = new Faker();

To generate data with different locales:

We use locales to make the generated data more specific to a single location.

Faker faker = new Faker(new Locale(“LOCALE_NAME”));

If you want to generate with different locales:

//e.g en-US
Faker usFaker= new Faker(new Locale("en-US"));
System.out.println(String.format("City in US: %s", usFaker.address.city()));
//e.g en-GB
Faker ukFaker= new Faker(new Locale("en-GB"));
System.out.println(String.format("City in UK: %s", ukFaker.address.city()));

Data for user profile:

Faker faker=new Faker();faker.name().fullName();
faker.name().firstName();
faker.name().lastName();
faker.job().position();
faker.phoneNumber().cellPhone();
faker.internet().emailAddress()
faker.address().streetAddress();
faker.address().cityName();
faker.address().country();

Data for date:

Faker faker=new Faker();faker.date().birthday();
faker.date().future(1, TimeUnit.DAYS);
faker.date().past(1, TimeUnit.DAYS);
faker.date().future(1, TimeUnit.DAYS);

Data for library app:

Faker faker=new Faker();faker.book().title();
faker.book().author();
faker.book().publisher();

Data for fin-tech app:

Faker faker=new Faker();faker.currency().name();
faker.currency().code();

Data for company profile:

Faker faker=new Faker();faker.company().name());
faker.company().suffix();
faker.company().bs();
faker.company().buzzword();
faker.company().logo();
faker.company().profession();
faker.company().industry();
faker.company().url();

Data based on demographics:

Faker faker=new Faker();faker.demographic().sex();
faker.demographic().demonym();
faker.demographic().educationalAttainment();
faker.demographic().maritalStatus();
faker.demographic().race();

Data based on the locale:

Faker faker = new Faker(new Locale("en-US"));
faker.name().fullName();
faker.name().firstName();
faker.name().lastName();

Conclusion

In this article, we explored how we can use this library to generate some random data across various domains and locale. This library is useful to do development and also validation with dummy data.

--

--

Thameem Ansari

Technology Expert| Coder| Sharing Experience| Spring | Java | Docker | K8s| DevOps| https://reachansari.com