Try Easy Ramdom, a PropertyBase Testing tool for java

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"
}

Basic

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)

Change the generation method

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)

Used with Bean Validation

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

Try Easy Ramdom, a PropertyBase Testing tool for java
Modern best practices for Java testing
Create a fluentd server for testing
Try running a Kubernetes Job from Java
Try making a calculator app in Java
I made a new Java deployment tool
A story about Java 11 support for Web services
Try to create a bulletin board in Java
A tool for hitting arbitrary SQL using JDBC
[Java] Let's create a mod for Minecraft 1.14.4 [Introduction]
[Java] Let's create a mod for Minecraft 1.16.1 [Introduction]
Create a tool for name identification in Salesforce
A collection of simple questions for Java beginners
[Java] Let's create a mod for Minecraft 1.14.4 [99. Mod output]
Build a development environment for Docker, java, vscode
Try debugging a Java program with VS Code
Compare PDF output in Java for snapshot testing