Quick introduction to EasyRandom
Tags: java, junit
Published: 2023-04-18 22:55:33

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5shnaqf6syh1k4rx0g3k.png)

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q1hbtwkfk3sbtum9m560.png)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/311du64govjtdvx3w2u1.png)

This method will generate a list of 10 employees.  

## Complex object

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


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/enehw1bq0ab6782vttz2.png)

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: 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eyx64zchobzvxvfdfpkt.png)

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.  

###