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 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:
This is a simple POJO, that has no nested objects/dependencies.
Generating a single object will be done through the nextObject method:
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:
This method will generate a list of 10 employees.
Great!!! now let's put more complexity in our model:
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, .................
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:
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)])]
That's it for this quick introduction to EasyRandom. For more details please refer to the official documentation.