Class InstancioExtension

java.lang.Object
org.instancio.junit.InstancioExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.AfterTestExecutionCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension

public class InstancioExtension extends Object implements org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.AfterTestExecutionCallback
Instancio JUnit extension.

Adds support for reporting Random Number Generator's seed value in case of a test failure. This allows the failed test to be reproduced by re-running it with the same seed value.

For example, given the following test class:


     @ExtendWith(InstancioExtension.class)
     class ExampleTest {

         @Test
         void verifyPerson() {
             Person person = Instancio.create(Person.class);
             // some test code...
             // ... some assertion fails
         }
     }
 

The failed test will report the seed value that was used, for example: "Test method 'verifyPerson' failed with seed: 12345".

Subsequently, the failing test can be reproduced by annotating the test method with the Seed annotation:


         @Test
         @Seed(12345) // will reproduce previously generated data
         void verifyPerson() {
             Person person = Instancio.create(Person.class);
             // snip...
         }
 
  • Constructor Details

    • InstancioExtension

      public InstancioExtension()
      Default constructor; required for JUnit extensions.
  • Method Details

    • beforeEach

      public void beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      beforeEach in interface org.junit.jupiter.api.extension.BeforeEachCallback
    • afterEach

      public void afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      afterEach in interface org.junit.jupiter.api.extension.AfterEachCallback
    • afterTestExecution

      public void afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      afterTestExecution in interface org.junit.jupiter.api.extension.AfterTestExecutionCallback