Generate Automation Test Data Using Java Faker Library

Apoorva Uthra
3 min readOct 1, 2023

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 ,address or new email ID. This is possible using Faker library.

Dependency

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

Faker API in Java

Faker API will generate unique fake/test data. Test data generated using this API can help testers in testing the application when there is a need for unique test data. By using Faker, we can generate distinct categories of data (i.e., address, color, date & time, name, phone number, etc.). Further, test data generated can be country specific as well. Please click on the link for detailed information.

Sample Program using Faker API in Java

import java.util.Locale;
import com.github.javafaker.Faker;
public class FakerTestDataGenerator {
public static void main(String[] args) {
Locale locale = new Locale("en-IND"); // It will generate India specific data.
Faker faker = new Faker(locale);
String fullName = faker.name().fullName();
String firstName = faker.name().firstName();
String lastName = faker.name().lastName();
String address = faker.address().streetAddress();
String phoneNumber = faker.phoneNumber().phoneNumber();
String email = faker.internet().emailAddress();
System.out.println("The Name is : " + fullName);
System.out.println("The First Name is : " + firstName);
System.out.println("The Last Name is : " + lastName);
System.out.println("The Address is : " + address);
System.out.println("The Address is : " + phoneNumber);
System.out.println("The EMail is : " + email);
}
}

FakeValueService

This class provides methods for generating random sequences. Let us see some of the useful methods provided by the FakeValueService class.

  1. Letterify :

This method helps to generate random sequences of alphabetic characters.

2. Numerify :

This method helps to generate numeric sequences.

3. Bothify :

This method is a combination of the two and can create random alphanumeric sequences — useful for mocking contents like ID strings.

4. Regexify :

This method generates random sequence based on a given regex pattern.

FakeValueService requires a valid Locale, as well as a RandomService:

In the below test, we create a FakeValueService instance with locale ‘en-GB’ and generate a unique fake gmail address with the help of Bothify method.

Here, we replace ‘?’ by random letters and ‘#’ by random numbers and then validate the generated gmail address with the help of Matcher.

In the below code snippet, we will use FakeValueService to create a random sequence on the basis of a specified regex using the Regexify method.

Conclusion

In this article, we explored the JavaFaker library to generate real-looking fake data along with the useful classes that is Faker class and FakeValueService class.

--

--