Easy Random/Faker JUnit 5 extension
The simple, stupid random Java™ beans generator for JUnit 5
The simple, stupid random Java™ beans generator for JUnit 5
The easy random extension provides a test with randomly generated objects, including:
- JDK types: int/double/BigDecimal/boolean/String etc
- Custom types: POJO, except for records support
- Generic collections: List/Set/Stream/Array
- Java Faker support: Name, Internet, Address etc
- Data Faker support: Name, Internet, Address etc
- Javax/Jakarta Validation annotations: @Email, @Pattern etc
- Custom Annotation with Validation annotation, such as @Phone. For more https://any86.github.io/any-rule/
@Documented
@Constraint(validatedBy = {})
@Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
@Retention(RUNTIME)
@Pattern(regexp = "^1[3-9]\\d{9}$")
public @interface Phone {
}
- i18n friendly to Faker's types
import com.github.javafaker.Address
@Test
public void testAddress(@Random(locale = "zh_CN") Address address) {
System.out.println(address.cityName());
}
Include following dependency in your pom.xml
<dependency>
<groupId>org.mvnsearch</groupId>
<artifactId>easy-random-junit5-extension</artifactId>
<version>0.5.0</version>
<!--<version>0.2.0</version> for Java 8 & 11-->
<scope>test</scope>
</dependency>
- Injecting random values as fields:
import org.jeasy.random.EasyRandomExtension;
import org.jeasy.random.Random;
@ExtendWith(EasyRandomExtension.class)
public class MyTest {
@Random
private String anyString;
@Random
private List<DomainObject> domainObjectList;
@Test
public void testUsingRandomString() {
// use the injected anyString
// ...
}
@Test
public void testUsingRandomDomainObjects() {
// use the injected anyDomainObjects
// the anyDomainObjects will contain _N_ fully populated random instances of DomainObject
// ...
}
@Test
public void testUsingPartiallyPopulatedDomainObject() {
// use the injected anyPartiallyPopulatedDomainObject
// this object's "name" and "value" members will not be populated since this has been declared with
// excluded = {"name", "value"}
// ...
}
}
- Injecting random values as parameters:
@ExtendWith(EasyRandomExtension.class)
public class MyTest {
@Test
public void testUsingRandomString(@Random @Email String anyString) {
// use the provided anyString
// ...
}
@Test
public void testUsingRandomDomainObjects(@Random List<DomainObject> anyDomainObjects) {
// use the injected anyDomainObjects
// the anyDomainObjects will contain _N_ fully populated random instances of DomainObject
// ...
}
}
With Automatic Extension Registration, and you can remove @ExtendWith(EasyRandomExtension.class)
on test class.
- Create
src/test/resources/junit-platform.properties
with following code:
junit.jupiter.extensions.autodetection.enabled=true
- Remove
@ExtendWith(EasyRandomExtension.class)
on test class
- Easy Random: https://github.com/j-easy/easy-random
- RandomBeansExtension: https://glytching.github.io/junit-extensions/randomBeans
- Instancio: a Java library for automating data setup in unit tests https://www.instancio.org/