DEV Community

loading...

Using EasyRandom with Spring Framework

namuan profile image namuan Originally published at deskriders.dev ・1 min read

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

Discussion (0)

pic
Editor guide