Introduction
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:-
com.github.javafaker javafaker 1.0.2
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
package FakerClassExamples; import com.github.javafaker.Faker; public class FakerWithLocale { public static void main(String[] args) { // Create an object of Faker class with default locale i.e ENG Faker faker = new Faker(); // To generate valid random first name System.out.println("First name is: "+faker.name().firstName()); // To generate valid random last name System.out.println("Last name is: "+faker.name().lastName()); // To generate valid random city name System.out.println("City name is: "+faker.address().cityName()); // To generate valid random state name System.out.println("State name is: "+faker.address().state()); // To generate valid random country name System.out.println("Country name is: "+faker.address().country()); // To generate valid random cell number System.out.println("Cell number is: "+faker.phoneNumber().cellPhone()); // To generate valid random animal name System.out.println("Animal name is: "+faker.animal().name()); } }
Output
First name is: Jay Last name is: Mraz City name is: Rashadburgh State name is: Arkansas Country name is: Bahrain Cell number is: 958-668-7313 Animal name is: bee
Second run
First name is: Alyssa Last name is: Carter City name is: Jodyfort State name is: Connecticut Country name is: Puerto Rico Cell number is: 1-546-542-1758 Animal name is: turtle
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.
package FakerClassExamples; import java.util.Locale; import com.github.javafaker.Faker; public class FakerWithIndiaLocale { public static void main(String[] args) { // Create an object of Faker class with default locale i.e ENG Faker faker = new Faker(new Locale("en-IND")); // To generate valid random first name System.out.println("First name is: "+faker.name().firstName()); // To generate valid random last name System.out.println("Last name is: "+faker.name().lastName()); // To generate valid random city name System.out.println("City name is: "+faker.address().cityName()); // To generate valid random state name System.out.println("State name is: "+faker.address().state()); // To generate valid random country name System.out.println("Country name is: "+faker.address().country()); // To generate valid random cell number System.out.println("Cell number is: "+faker.phoneNumber().cellPhone()); // To generate valid random animal name System.out.println("Animal name is: "+faker.animal().name()); } }
Output
First name is: Rageshwari Last name is: Sethi City name is: Kolkata State name is: Dadar and Nagar Haveli Country name is: Saint Vincent and the Grenadines Cell number is: (063) 036-5884 Animal name is: minnow =================================== First name is: Kin Last name is: Mishra City name is: Hyderabad State name is: Tamil Nadu Country name is: South Africa Cell number is: 572.064.1603 Animal name is: walrus
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.
#ThanksForReading
#HappyLearning
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.
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.
Hi Amod,
You can also have the SdGen library integrated with the faker library to create a CSV/excel file as required.
au.com.anthonybruno
SdGen
0.3.0
We can create CSV files containing n number of fake data records.