DEV Community

BOUFATAH Amine
BOUFATAH Amine

Posted on

Quick introduction to EasyRandom

Hey Folks ,

Today I'd like to give some words about an interesting library that I have recently discovered and which I'd wish I had known before. This library called EasyRandom is useful when writing test code/sample code.

EasyRandom simply put

EasyRandom is a library that generates Java beans. It is mainly used to help create example objects that you use for testing.

Sometimes while we are writing test code, we find ourselves writing a lot of sample data to test our components, it may be tedious to write such data especially if the object tree is way too big, think of a Person object having one address object, a List of Employers, a Department and so on.. Writing sample data for this scenario is tiresome and uninteresting, this is exactly when EasyRandom comes in handy.

Let's dive into a simple example, suppose we have a java object representing an Employee as follows:

Image description

This is a simple POJO, that has no nested objects/dependencies.

Single object

Generating a single object will be done through the nextObject method:

Image description

A list of objects

What if we wanted a list of Employees to be generated ? simple, use the objects method which will return a Stream of the desired objects:

Image description

This method will generate a list of 10 employees.

Complex object

Great!!! now let's put more complexity in our model:

Image description

The object generated after calling nextObject looks like the following:

[Employee(firstName=eOMtThyhVNLWUZNRcBaQKxI, lastName=yedUsFwdkelQbxeTeQOvaScfqIOOmaa, hiringDate=2024-06-18, contactNumber=JxkyvRnL, department=Department(name=RYtGKbgicZaHCBRQDSx), manager=Manager(managerLevel=VLhpfQGTMDYpsBZxvfBoeygjb), position=Position(positionCode=oDLDPR), previousEmployers=[Employer(employerName=IQrWtYnHcO, contactNumber=XgLyqkDjxtCDIj), Employer(employerName=zQeAydAJIwkGuKWiVZLRcElhreuxvw, contactNumber=YVeSelxWdEtYtJHV), Employer(employerName=VIhYOTAybWXyBzGqRe, contactNumber=wKkOqxJSqDjAtcDF), Employer(employerName=hXJISTd, contactNumber=cdpW), Employer(employerName=hXJISTd, contactNumber=cdpW), Employer(employerName=hXJISTd, contactNumber=cdpW), Employer(employerName=GGiDYDmCRViOaAXNumPGEBX, contactNumber=O), Employer(employerName=bplgXS, contactNumber=PAgcjtZAOBahwMGwNikIaKuxYYVASJc), Employer(employerName=RRVabC, contactNumber=DwmrfUyJVSbdBFIyjTfpDItaXB), Employer(employerName=bplgXS, 
.................

Enter fullscreen mode Exit fullscreen mode

We could see from the result shown above that the library created many Employer objects for our Employee, in my case it created 48, maybe we do not need that many employers for our tests, and that's where EasyParameters comes in. EasyParameters is an object which enables us customize the creation of our objects:

Image description

Through the EasyRandomParameters I've chosen to generate at max 3 items in a collection, I've also set the range of dates that should be picked when generating a random Date.

Run the program and this time the result is more convenient:

[Employee(firstName=eOMtThyhVNLWUZNRcBaQKxI, lastName=yedUsFwdkelQbxeTeQOvaScfqIOOmaa, hiringDate=2022-05-19, contactNumber=JxkyvRnL, department=Department(name=RYtGKbgicZaHCBRQDSx), manager=Manager(managerLevel=VLhpfQGTMDYpsBZxvfBoeygjb), position=Position(positionCode=GGiDYDmCRViOaAXNumPGEBX), previousEmployers=[Employer(employerName=JuhuUTlSCYdTVoSsJGyClHNnCWZGwZ, contactNumber=ImfitPVWqHXHNeZIUAYI)])]

Enter fullscreen mode Exit fullscreen mode

That's it for this quick introduction to EasyRandom. For more details please refer to the official documentation.

Top comments (1)

Collapse
 
armandino profile image
armandino

Nice article! However Easy Random is no longer maintained. For those looking for an actively maintained project, consider using Instancio. Disclaimer: I'm the maintainer of this library