top of page

Download free e-books

Explore the world of Software and Data Engineering in a more efficient and accessible way with our eBooks!

  • Writer's pictureJP

How to generate random Data using Datafaker lib

Sometimes in our projects we have to fill Java objects for unit tests or even to create a database dump with random data to test a specific feature and etc. We need to be creative trying to create names, street names, cities or documents.


There's an interesting and helpful Java library called Datafaker that allows to create random data with a large number of providers.


Providers are objects based on a context, for example:


If you want to generate data about a Person object, there's a specific provider for this context that will generate name, last name and etc. If you need to create a unit test that you need data about address, you'll find it.


In this post we'll create some examples using Maven but the library also provides support for Gradle projects.


Maven

<dependency>
    <groupId>net.datafaker</groupId>
    <artifactId>datafaker</artifactId>
    <version>1.1.0</version>
</dependency>

Generating Random Data


Let's create a simple Java class that contains some properties like name, last name, address, favorite music genre and food.

public class RandomPerson {

    public String firstName;
    public String lastName;
    public String favoriteMusicGenre;
    public String favoriteFood;
    public String streetAddress;
    public String city;
    public String country;
    
    @Override
    public String toString() {
    
        return  "firstName=" + firstName + "\n" +
                "lastName=" + lastName + "\n" +
                "favoriteMusicGenre="+favoriteMusicGenre + "\n" +
                "favoriteFood=" + favoriteFood + "\n" +
                "streetAddress=" + streetAddress + "\n" +
                "city=" + city + "\n" +
                "country=" + country ;
    }
    
    static void print(RandomPerson randomPerson){
        System.out.println(
            randomPerson
        );
    }
}

In the next step we'll fill an object using the providers that we quote in the first section. First of all, we create an object called randomData that represents Faker class. This class contains all the providers in the example below.

public static void main(String[] args) {

    Faker randomData
            = new Faker();

    RandomPerson randomPerson
            = new RandomPerson();
            
    randomPerson.firstName
            = randomData.name().firstName();
            
    randomPerson.lastName
            = randomData.name().lastName();

    randomPerson.favoriteMusicGenre
            = randomData.music().genre();

    randomPerson.favoriteFood
            = randomData.food().dish();

    randomPerson.streetAddress
            = randomData.address().streetAddress();

    randomPerson.city
            = randomData.address().city();

    randomPerson.country
            = randomData.address().country();

    print(randomPerson);  
}

After the execution, we can see the results like this at the console:


Result


firstName=Dorthy

lastName=Jones

favoriteMusicGenre=Electronic

favoriteFood=Cauliflower Penne

streetAddress=7411 Darin Gateway

city=Gutkowskifort

country=Greece


Every execution will be a new result because of providers are randoms. Another interesting feature is that we can set up the Locale when instantiate an object.

Faker randomData
        = new Faker(Locale.JAPANESE);

See the results based on Local.JAPANESE:


Result


firstName=航

lastName=横山

favoriteMusicGenre=Non Music

favoriteFood=French Fries with Sausages

streetAddress=418 美桜Square

city=南斉藤区

country=Togo


 

Books to study and read


If you want to learn more about and reach a high level of knowledge, I strongly recommend reading the following book(s):


Unit Testing Principles, Practices, and Patterns: Effective Testing Styles, Patterns, and Reliable Automation for Unit Testing, Mocking, and Integration Testing with Examples in C# is a book that covers Unit Testing Principles, Patterns and Practices teaches you to design and write tests that target key areas of your code including the domain model. In this clearly written guide, you learn to develop professional-quality tests and test suites and integrate testing throughout the application life cycle.













Mastering Unit Testing Using Mockito and JUnit is a book that covers JUnit practices using one of the most famous testing libraries called Mockito. This book teaches how to create and maintain automated unit tests using advanced features of JUnit with the Mockito framework, continuous integration practices (famous CI) using market tools like Jenkins along with one of the largest dependency managers in Java projects, Maven. For you who are starting in this world, it is an excellent choice.














Isn't a cool library!? See you!


















Commentaires


bottom of page