JUnit 5 extension for easy-random

Overview
Easy Random/Faker JUnit 5 extension
The simple, stupid random Java™ beans generator for JUnit 5

MIT license Build Status Maven Central Project status

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
  • Java 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());
}

How to use?

Include following dependency in your pom.xml

  <dependency>
    <groupId>org.mvnsearch</groupId>
    <artifactId>easy-random-junit5-extension</artifactId>
    <version>0.3.0</version>
    <!--<version>0.2.0</version> for Java 8 & 11-->
    <scope>test</scope>
  </dependency>

Usage examples:

  • 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
           // ...
       }
   }

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:
junit.jupiter.extensions.autodetection.enabled=true
  • Remove @ExtendWith(EasyRandomExtension.class) on test class

References and Thanks

You might also like...

Testes unitários em Java utilizando JUnit

Testes unitários em Java utilizando JUnit Este curso tem como objetivo habilitar o(a) aluno(a) a testar soluções desenvolvidas na linguagem Java, torn

Nov 8, 2022

Intercept network request by running Selenium tests with JUnit on LambdaTest cloud.

Intercept network request by running Selenium tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Override device mode by running Selenium test with JUnit on LambdaTest cloud.

Override device mode by running Selenium test with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Emulate geolocation by running Selenium tests with JUnit on LambdaTest cloud.

Emulate geolocation by running Selenium tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Testes unitários em Java utilizando JUnit

Testes unitários em Java utilizando JUnit Este curso tem como objetivo habilitar o(a) aluno(a) a testar soluções desenvolvidas na linguagem Java, torn

Oct 13, 2022

Run Selenium 4 tests with JUnit on LambdaTest cloud.

Run Selenium 4 tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Check performance metrics by running Selenium 4 tests with JUnit on LambdaTest cloud.

Check performance metrics by running Selenium 4 tests with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Capture JavaScript error by running Selenium test with JUnit on LambdaTest cloud.

Capture JavaScript error by running Selenium test with JUnit on LambdaTest cloud.

Run Selenium 4 Tests With JUnit On LambdaTest Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube       Learn how to use JUnit framewor

Jul 11, 2022

Clear browser cache by running Selenium tests with JUnit on LambdaTest cloud.

Clear browser cache by running Selenium tests with JUnit on LambdaTest cloud.

Run Selenium Tests With JUnit On LambdaTest (Browser Cache Clearing Example) Blog ⋅ Docs ⋅ Learning Hub ⋅ Newsletter ⋅ Certifications ⋅ YouTube      

Jul 11, 2022
Releases(0.3.0)
  • 0.3.0(Oct 19, 2021)

    • JDK 17 only: for Java 8 & 11, please use version 0.2.0
    • Java Records support
        record Person(@Positive int id, @Email String email) {
        }
    
        @Test
        public void testRecord(@Random Person person) {
            System.out.println("person.id = " + person.id());
            System.out.println("person.email = " + person.email());
        }
    
    Source code(tar.gz)
    Source code(zip)
  • 0.2.0(Sep 12, 2021)

    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());
    }
    
    Source code(tar.gz)
    Source code(zip)
  • 0.1.2(Sep 12, 2021)

    Custom Annotation with Validation annotation, such as @Phone

    @Documented
    @Constraint(validatedBy = {})
    @Target({METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER, TYPE_USE})
    @Retention(RUNTIME)
    @Pattern(regexp = "(136|137|186)\\d{8}")
    public @interface Phone {
    }
    
    Source code(tar.gz)
    Source code(zip)
  • 0.1.1(Sep 5, 2021)

  • 0.1.0(Sep 5, 2021)

    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 Validation annotations: @Email, @Pattern etc
    Source code(tar.gz)
    Source code(zip)
Owner
Libing Chen
FullStack software engineer, with 20+ years experience as Java developer
Libing Chen
A TestNG like dataprovider runner for JUnit with many additional features

junit-dataprovider Have a look to this repositories Wiki pages for documenation, usage examples and further information. Table of contents Home What i

TNG Technology Consulting GmbH 237 Sep 7, 2022
Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium web browsers, or anything else that can run in a Docker container.

Testcontainers Testcontainers is a Java library that supports JUnit tests, providing lightweight, throwaway instances of common databases, Selenium we

null 6.7k Jan 9, 2023
CodeSheriff is a simple library that helps you in writing JUnit tests that check the quality of your code

CodeSheriff is a simple library that helps you in writing JUnit tests that check the quality of your code. For example, CodeSheriff may fail because you have methods in your code that have more than X lines of code, or that have complexity greater than Y.

Maurício Aniche 62 Feb 10, 2022
JUnit 5 Parameterized Test Yaml Test Data Source

Yamaledt — JUnit 5 Parameterized Tests Using Yaml and Jamal Introduction and usage Note This is the latest development documentation. This is a SNAPSH

Peter Verhas 4 Mar 23, 2022
Exercicio envolvendo criação de Classe, Junit 5 e exception

ClasseData Complemente a classe Data desenvolvida anteriormente para que lance uma ExcecaoDataInvalida. Com esta única exceção, você deverá ser capaz

Mateus Samartini 3 May 7, 2021
Exercico com Junit 5, UML, Interação de classes e Exception

AcessoClube Exercico com Junit 5, UML, Interação de classes e Exception Exercicio: Projete e desenvolva um sistema de controle de acesso a um clube co

Mateus Samartini 3 May 3, 2021
This is a repository to collect JUnit Tests for GAD exercises at TUM in SS21

TUM_GAD_Tests_SS21 This is a repository to collect JUnit Tests for GAD exercises at TUM in SS21. These tests have been written by students for student

null 41 Oct 31, 2022
A template for Spring Boot REST API tested with JUnit 5 and Cucumber 6

demo-bdd Un template Spring Boot pour lancer un BDD/ATDD avec Cucumber 6 et JUnit 5. Maven et le JDK 17 seront nécessaires. Exécuter les tests Le proj

Rui Lopes 4 Jul 19, 2022
IntelliJ IDEA and JUnit: Writing, Finding, and Running Tests

IntelliJ IDEA and JUnit: Writing, Finding, and Running Tests ?? Webinar https://blog.jetbrains.com/idea/2021/11/live-stream-recording-intellij-idea-an

Christian Stein 11 Jul 23, 2022
Testes unitários em Java utilizando JUnit 5

Testes unitários em Java utilizando JUnit Este curso tem como objetivo habilitar o(a) aluno(a) a testar soluções desenvolvidas na linguagem Java, torn

Camila Cavalcante 3 Jan 18, 2022