Class DbUnitExtension

java.lang.Object
com.github.mjeanroy.dbunit.integration.jupiter.DbUnitExtension
All Implemented Interfaces:
org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.Extension, org.junit.jupiter.api.extension.ParameterResolver, org.junit.jupiter.api.extension.TestInstantiationAwareExtension

public class DbUnitExtension extends Object implements org.junit.jupiter.api.extension.BeforeAllCallback, org.junit.jupiter.api.extension.AfterAllCallback, org.junit.jupiter.api.extension.BeforeEachCallback, org.junit.jupiter.api.extension.AfterEachCallback, org.junit.jupiter.api.extension.ParameterResolver
A simple JUnit Jupiter extension for DbUnit.
Basically, this class will:
  • Read database configuration, configured with DbUnitConnection, before all tests.
  • Run (optional) initialization scripts, configured with DbUnitInit, before all tests.
  • Run (optional) liquibase changelogs, configured with DbUnitLiquibase, before all tests.
  • Execute DbUnit SETUP operation, configured with DbUnitSetup, before each test.
  • Load dataset, configured with DbUnitDataSet, before each test.
  • Execute DbUnit TEARDOWN operation, configured with DbUnitTearDown, before each test.
Note that is you are using Spring Test Framework in your application, this extension can be combined with EmbeddedDatabaseExtension to initiate an embedded database (HSQL or H2 database).
This extension can also be used with RegisterExtension annotation as a static field or as an instance field.
Here is an example:

  @ExtendWith({EmbeddedDatabaseExtension.class, DbUnitExtension.class})
  @DbUnitConnection(url = "jdbc:hsqldb:mem:testdb", user = "SA", password = "")
  @bUnitDataSet("classpath:/dataset/xml")
  class MyDaoTest {
    @Test
    void test1() {
      // ...
    }
  }

See Also:
  • Nested Class Summary

    Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension

    org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope
  • Constructor Summary

    Constructors
    Constructor
    Description
    Create the extension.
    Create rule using JdbcConfiguration instance.
    Create rule using JdbcConnectionFactory to create SQL Connection.
    Create rule using DataSource to create SQL Connection.
  • 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
    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)
     

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

    Methods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension

    getTestInstantiationExtensionContextScope
  • Constructor Details

    • DbUnitExtension

      public DbUnitExtension()
      Create the extension.
    • DbUnitExtension

      public DbUnitExtension(JdbcConfiguration configuration)
      Create rule using JdbcConfiguration instance. This constructor should be used with RegisterExtension annotation.
      Parameters:
      configuration - JDBC Configuration.
    • DbUnitExtension

      public DbUnitExtension(JdbcConnectionFactory factory)
      Create rule using JdbcConnectionFactory to create SQL Connection. This constructor should be used with RegisterExtension annotation.
      Parameters:
      factory - JDBC Configuration.
    • DbUnitExtension

      public DbUnitExtension(DataSource dataSource)
      Create rule using DataSource to create SQL Connection. This constructor should be used with RegisterExtension annotation.
      Parameters:
      dataSource - The datasource to use.
  • Method Details

    • beforeAll

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

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

      public boolean supportsParameter(org.junit.jupiter.api.extension.ParameterContext parameterContext, org.junit.jupiter.api.extension.ExtensionContext extensionContext) throws org.junit.jupiter.api.extension.ParameterResolutionException
      Specified by:
      supportsParameter in interface org.junit.jupiter.api.extension.ParameterResolver
      Throws:
      org.junit.jupiter.api.extension.ParameterResolutionException
    • resolveParameter

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