An open API service indexing awesome lists of open source software.

https://github.com/linux-china/easy-random-junit5-extension

JUnit 5 extension for easy-random
https://github.com/linux-china/easy-random-junit5-extension

easy-random junit-extension junit5

Last synced: about 1 month ago
JSON representation

JUnit 5 extension for easy-random

Awesome Lists containing this project

README

        


Easy Random/Faker JUnit 5 extension

The simple, stupid random Java™ beans generator for JUnit 5

[![MIT license](https://img.shields.io/badge/license-Apache-brightgreen.svg?style=flat)](http://opensource.org/licenses/Apache-2.0)
[![Build Status](https://github.com/linux-china/easy-random-junit5-extension/actions/workflows/main.yml/badge.svg)](https://github.com/linux-china/easy-random-junit5-extension/actions)
[![Maven Central](https://img.shields.io/maven-central/v/org.mvnsearch/easy-random-junit5-extension)](https://repo1.maven.org/maven2/org/mvnsearch/easy-random-junit5-extension/)
[![Project status](https://img.shields.io/badge/Project%20status-Maintenance-orange.svg)](https://img.shields.io/badge/Project%20status-Maintenance-orange.svg)

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](https://github.com/j-easy/easy-random/issues/397)
* Generic collections: List/Set/Stream/Array
* [Java Faker](https://github.com/DiUS/java-faker) support: Name, Internet, Address etc
* [Data Faker](https://github.com/datafaker-net/datafaker/) 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/

```java
@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

```groovy
import com.github.javafaker.Address

@Test
public void testAddress(@Random(locale = "zh_CN") Address address) {
System.out.println(address.cityName());
}
```

# How to use?

Include following dependency in your pom.xml

```xml

org.mvnsearch
easy-random-junit5-extension
0.5.0

test

```

# Usage examples:

* Injecting random values as fields:

```java
import org.jeasy.random.EasyRandomExtension;
import org.jeasy.random.Random;

@ExtendWith(EasyRandomExtension.class)
public class MyTest {

@Random
private String anyString;

@Random
private List 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:

```java
@ExtendWith(EasyRandomExtension.class)
public class MyTest {

@Test
public void testUsingRandomString(@Random @Email String anyString) {
// use the provided anyString
// ...
}

@Test
public void testUsingRandomDomainObjects(@Random List anyDomainObjects) {
// use the injected anyDomainObjects
// the anyDomainObjects will contain _N_ fully populated random instances of DomainObject
// ...
}
}
```

# JUnit 5 Automatic Extension Registration

With Automatic Extension Registration, and you can remove `@ExtendWith(EasyRandomExtension.class)` on test class.

* Create `src/test/resources/junit-platform.properties` with following code:

```properties
junit.jupiter.extensions.autodetection.enabled=true
```

* Remove `@ExtendWith(EasyRandomExtension.class)` on test class

# References and Thanks

* 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/