Easy Ramdom is a tool that randomly creates a Java instance. A type of Property Base Testing tool.
https://github.com/j-easy/easy-random
The feature is like this --Can be generated without getter / setter / default constructor (I tried it with groovy's spock. I didn't need it with spock. I haven't tried it with JUnit. Just in case.) —— Creates data according to the type --Complex types are OK ――Simple --The generation method can be changed flexibly --Enum randomly selects from the value of Enum --When used in combination with Bean Validation, data will be created within the limits of Validation.
It's an ideal tool for me.
There is only one dependency. Also easy-random-bean-validation when using Bean Validation.
dependencies{
testCompile "org.jeasy:easy-random-core:4.0.0.RC1"
testCompile "org.jeasy:easy-random-bean-validation:4.0.0.RC1"
}
EasyRandom easyRandom = new EasyRandom()
Quantity quantity = easyRandom.nextObject(Quantity.class)
This alone will create a Quantity object. The contents of Quantity will be made according to the type.
Seed seems to be fixed, so basically it returns the same value every time. Therefore, it is recommended to specify seed.
EasyRandomParameters parameters = new EasyRandomParameters().seed(System.currentTimeMillis())
EasyRandom easyRandom = new EasyRandom(parameters)
Quantity quantity = easyRandom.nextObject(Quantity.class)
You can freely change the generation method by creating a Randomizer. The following example is designed to be generated within the range of (Reiwa !?) 1 to 1000.
Easy Ramdom also has a build in Randomizer, which seems to generate names and zip codes. It seems that it uses a tool called faker internally. It seems that Locale can also be specified, so it seems that Japanese names and addresses can be generated. I haven't tried it yet. To use it, you need to add easy-random-randomizers to dependencise.
QuantityRandmizer.java
public class QuantityRandmizer implements Randomizer<Integer>
{
@Override
public Integer getRandomValue()
{
return ThreadLocalRandom.current().nextInt(1, 1000 + 1);
}
}
EasyRandomParameters parameters = new EasyRandomParameters().seed(System.currentTimeMillis())
.randomize(FieldPredicates.named("value")
.and(FieldPredicates.ofType(Integer.class))
.and(FieldPredicates.inClass(Quantity.class)), new QuantityRandmizer())
EasyRandom easyRandom = new EasyRandom(parameters)
Quantity quantity = easyRandom.nextObject(Quantity.class)
It can also be generated within the scope of Bean Validation. In the example below, it will be generated in the range of 100 to 1000.
Quantity.java
public class Quantity
{
@Min(100)
@Max(1000)
Integer value;
public Quantity(Integer value)
{
this.value = value;
}
}
EasyRandomParameters parameters = new EasyRandomParameters().seed(System.currentTimeMillis())
EasyRandom easyRandom = new EasyRandom(parameters)
Quantity quantity = easyRandom.nextObject(Quantity.class)
Great!
Recommended Posts