Let's assume that there is a Java class object model in which all fields are annotated with JSR-303 constraints. Something like this:
@SuperBuilder
@ToString
@EqualsAndHashCode
public class Address {
@Size(min = 1, max = 20)
@NotNull
public final String line1;
@Size(min = 1, max = 20)
public final String line2;
@Size(min = 1, max = 20)
public final String city;
@Size(min = 1, max = 20)
public final String country;
@Size(min = 1, max = 20)
public final String zipCode;
}
and maybe few tens more like this:
@SuperBuilder
@ToString
@EqualsAndHashCode
public class Person {
@NotNull
@Valid
public final Phone phone;
@NotNull
@Valid
public final Address address;
}
Manually writing and maintaing test data for these is extremely tedious.
I believe that this manual process can be automated, let's say:
- minimum values: null for nullable fields, minimum value for numeric fields, minimum length for string/collection fields, and false for booleans
- maximum values: not null for nullable fields, maximum value for numeric fields, maximum length for string/collection fields, and true for booleans
- below minimum values: null for nullable fields... you get the rest :)
- above maximum values
Then the programmer should be able to call:
SomeMagicalGenerator.for(Person.class)
.generateMaximum() // returns a Stream<Person>
.distinct()
.limit(10)
and use it as TestNG DataProvider for example.
At first Instancio looked promising :) Checked it here: https://github.com/adrian-herscu/instancio-experiment
But... I failed to find a way to make it generate minimum and maximum values accoding to the annotations. In my dream if that would work, then making it generate out-range values should be workable too.
Any ideas? Suggestions? Do I miss something?
If you enable the
Keys.BEAN_VALIDATION_ENABLED
setting, then Instancio will generate valid objects using the constraints.See: https://www.instancio.org/user-guide/#bean-validation