DEV Community

namuan
namuan

Posted on • Originally published at deskriders.dev

Using EasyRandom with Spring Framework

Tired of creating objects in tests. Try using EasyRandom library. Here is how easily you can add it to a Spring Java project

Step 1: Add gradle/maven dependency

https://mvnrepository.com/artifact/org.jeasy/easy-random

Step 2: Declare it in test

Step 3: Use it to generate random data objects

Step 4: 😍

Tips:

Random initial seed

In the default setup, EasyRandom uses a default seed value to generate randam data.
Although it is quite simple to initiate EasyRandom with a random seed.

Enforcing javax.validation rules:

If an object is using javax.validation then there is an extension which enforces those rules when generating random objects.

Include https://github.com/j-easy/easy-random/wiki/bean-validation-support


Github: https://github.com/namuan/dev-rider-codesnippets/tree/master/spring-boot-snippets

Top comments (0)