• JP

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


Isn't a cool library!? See you!


















Posts recentes

Ver tudo