Generate Realistic Test Data in Java with JavaFaker

This tutorial shows how to use the JavaFaker library to quickly create realistic mock data for Java applications, covering Maven setup, bean definition, locale configuration, and a loop that produces ten sample user records with names, phone numbers, addresses, and university names.

Programmer DD
Programmer DD
Programmer DD
Generate Realistic Test Data in Java with JavaFaker

1. Introduction

The boss wants convincing demo data for a client presentation, and developers often find generating such data tedious. This article introduces a simple way to produce realistic fake data using JavaFaker.

2. JavaFaker

JavaFaker is a Java library designed to generate synthetic data for testing. Add the following Maven dependency to your project:

<dependency>
    <groupId>com.github.javafaker</groupId>
    <artifactId>javafaker</artifactId>
    <version>1.0.2</version>
</dependency>
The primary use case is to create mock test data, not to fabricate fraudulent information.

First, define a simple Java bean to hold user information:

/**
 * User info.
 */
@Data
public class UserInfo {
    /** Real name */
    private String realName;
    /** Mobile phone */
    private String cellPhone;
    /** University */
    private String universityName;
    /** City */
    private String city;
    /** Street address */
    private String street;
}

Instantiate a Faker object. For Chinese data: Faker fakerWithCN = new Faker(Locale.CHINA); For US data, change the locale: Faker fakerWithCN = new Faker(Locale.US); Generate ten mock user records using the faker:

for (int i = 0; i < 10; i++) {
    UserInfo userInfo = new UserInfo();
    userInfo.setRealName(fakerWithCN.name().fullName());
    userInfo.setCellPhone(fakerWithCN.phoneNumber().cellPhone());
    userInfo.setCity(fakerWithCN.address().city());
    userInfo.setStreet(fakerWithCN.address().streetAddress());
    userInfo.setUniversityName(fakerWithCN.university().name());
    System.out.println("userInfo = " + userInfo);
}

Resulting output looks like genuine data:

Using JavaFaker satisfies the boss's request while reducing the effort needed for test data preparation. Remember to use the generated data responsibly and not for dishonest purposes.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

mock datatest datajavafaker
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.