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
- Host: GitHub
- URL: https://github.com/linux-china/easy-random-junit5-extension
- Owner: linux-china
- License: apache-2.0
- Created: 2021-09-04T23:27:20.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2023-01-28T07:45:18.000Z (over 2 years ago)
- Last Synced: 2025-04-02T02:51:08.901Z (about 2 months ago)
- Topics: easy-random, junit-extension, junit5
- Language: Java
- Homepage:
- Size: 101 KB
- Stars: 13
- Watchers: 2
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Easy Random/Faker JUnit 5 extension
The simple, stupid random Java™ beans generator for JUnit 5[](http://opensource.org/licenses/Apache-2.0)
[](https://github.com/linux-china/easy-random-junit5-extension/actions)
[](https://repo1.maven.org/maven2/org/mvnsearch/easy-random-junit5-extension/)
[](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/