Make Selenium Easy

Javafaker API – Generate Real-Time Fake Data – Faker Class


I remember when I used to generate some random data with alphabets and numbers which were meaningless words like “Bjhet”, “Hyiue” etc. Generating some random data helps you to eliminate boilerplate codes or unnecessary headaches of maintaining data.

For example:- If you need to register a user which requires first name, last name, age, address, etc. We used to generate some random data for these fields so that there is no need to pass data explicitly. But these data were meaningless although it serves the purpose.

Will be not it good if we could generate fake data that looks like real data? Something a real name like “Amod” or “John”. Yes, It is possible using Java Faker API.

Javafaker API

Javafaker API is a Java library that provides utilities to generate pretty data for the showcase randomly. You can generate fake real data for name, address, city, state as per locale. If you want to get a random city name of US you can select US locale or if you want an Indian city use IND locale. Default locale it takes as “English”.

Maven Dependency

To use Javafaker library in your Java project we need to use the latest dependency of this library from the Maven central repo. For this post I have used the below version of this dependency:-

Faker class

The fake class provides utility methods for generating fake strings, such as names, phone numbers, addresses. generate random strings with given patterns. Let’s see a sample program to generate some data.

Example with the default locale


Second run

We can see how well it gives you real-time fake data.

Example with Indian locale

You need to pass a valid locale to the Faker constructor. You can find all supported locale here.


You can see it generates random data belonging to the IND locale. I see a problem here for interdependent data like generating a city name of a state. I mean to get a city name from state Karnataka rather than any city name.

We will see some more concepts about Javafaker in the upcoming post.

You can clone above Git Repo from here.

You can subscribe to my YouTube channel RetargetCommon to learn from video tutorials.

If you have any doubt, feel free to comment below.
If you like my posts, please like, comment, share and subscribe.

Find all Selenium related posts here, all API manual and automation related posts here, and find frequently asked Java Programs here.

Many other topics you can navigate through the menu.

Author: Amod Mahajan

A software Tester who is paid to judge products developed by others. Currently getting paid in American Dollars. Writing technical posts and creating YouTube videos are my hobbies.

2 thoughts on “Javafaker API – Generate Real-Time Fake Data – Faker Class

  1. Good point noted, about no interdependency.

    There should be a function where we can pass the value from one random function and it gives output based on the input provided.

    example if i pass lucknow(which is generated randomly) it should give us Uttar Pradesh instead of any random state

    Another observation cellnumber is not consistent and it comes sometimes with hyphen and sometimes with dot.

  2. Hi Amod,

    You can also have the SdGen library integrated with the faker library to create a CSV/excel file as required.

    We can create CSV files containing n number of fake data records.

Leave a Reply

Please wait...

Subscribe to new posts to become automation expert

Want to be notified when my new post is published? Get my posts in your inbox.