jFairy is a fake data generator. This JUnit extension provides simple injection of random data into fields and method parameters.
@Test
@ExtendWith(FairyExtension.class)
void withRandomPerson(@Random Person person) {
System.out.println(person.getFullName());
// Chloe Barker
}
This extension resolves fields and method parameters annotated with @Random
.
@ExtendWith(FairyExtension.class)
class MyTest {
@Random
private Person person1;
@Test
void example(@Random Person person2) {
// ...
}
}
The random generator can be customized with a locale and a seed.
@Random(locale = "de", seed = 1234)
-
boolean
andBoolean
@Random private boolean trueOrFalse;
-
int
andInteger
which can be customized using@IntegerWith
@Random @IntegerWith(min = 50, max = 100) private int i;
-
String
which can be customized using@StringWith
@Random @StringWith(maxLength = 20) private String randomString; @Random(locale = "de") @StringWith(type = WORD) private String germanWord;
-
Enum types
@Random private Month month;
-
Person
which can be customized using@PersonWith
@Random @PersonWith(sex = MALE, minAge = 13, maxAge = 19) private Person maleTeenager;
Look into the ExampleTests.