Class InstancioExtension

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

public class InstancioExtension extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.AfterTestExecutionCallback, org.junit.jupiter.api.extension.ParameterResolver
The Instancio JUnit extension adds support for additional features when using Instancio with JUnit Jupiter:
  • reporting the seed value to allow reproducing failed tests
  • injecting Settings using @WithSettings annotation
  • generating parameterized test arguments using @InstancioSource
  • injecting fields and method parameters using @Given aannotation

Reproducing failed tests

The extension generates a seed for each test method. When a test fails, the extension reports this seed in the output. Using the @Seed annotation, the test can be re-run with the reported seed to reproduce the data that caused the failure.

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

See the user guide for more details.

Since:
1.1.0
  • Constructor Summary

    Constructors
    Constructor
    Description
    Default constructor; required for JUnit extensions.
    InstancioExtension(org.instancio.support.ThreadLocalRandom threadLocalRandom, org.instancio.support.ThreadLocalSettings threadLocalSettings)
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    afterEach(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    afterTestExecution(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    beforeAll(org.junit.jupiter.api.extension.ExtensionContext context)
     
    void
    beforeEach(org.junit.jupiter.api.extension.ExtensionContext context)
     
    resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
     
    boolean
    supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
    For methods, JUnit invokes (1) beforeEach(), (2) resolveParameter() For constructors, the order is reverse.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • InstancioExtension

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

      InstancioExtension(org.instancio.support.ThreadLocalRandom threadLocalRandom, org.instancio.support.ThreadLocalSettings threadLocalSettings)
  • Method Details

    • beforeAll

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

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

      public void afterAll(org.junit.jupiter.api.extension.ExtensionContext context)
      Specified by:
      afterAll in interface org.junit.jupiter.api.extension.AfterAllCallback
    • 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
    • supportsParameter

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      For methods, JUnit invokes (1) beforeEach(), (2) resolveParameter() For constructors, the order is reverse. As a result, when resolveParameter() is called, the setup logic hasn't been run yet. For this reason, constructor parameters are not supported.
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
    • resolveParameter

      public Object resolveParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext)
      Specified by:
      resolveParameter in interface org.junit.jupiter.api.extension.ParameterResolver