Class AbstractDbTest

java.lang.Object
com.reallifedeveloper.tools.test.database.dbunit.AbstractDbTest

@Transactional public abstract class AbstractDbTest extends Object
Base class for DBUnit tests.

An example of how to use this class:

 @ExtendWith(SpringExtension.class)
 @ContextConfiguration(classes = TestConfiguration.class)
 public class MyDbTest extends AbstractDbTest {
     @Autowired
     private DataSource ds;

     @Autowired
     private IDataTypeFactory dataTypeFactory;

     public MyDbTest() {
         super(null, "/dbunit/myschema.dtd", "/dbunit/dataset1.xml", "/dbunit/dataset2.xml");
     }

     @Test
     public void myTest() {
         ...
     }

     @Override
     protected DataSource getDataSource() { return ds; }

     @Override
     protected IDataTypeFactory getDataTypeFactory() { return dataTypeFactory; }
 }
 
Author:
RealLifeDeveloper
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    static final org.dbunit.operation.DatabaseOperation
    The default operation to perform before executing each test.
    static final org.dbunit.operation.DatabaseOperation
    The default operation to perform after executing each test.
  • Constructor Summary

    Constructors
    Modifier
    Constructor
    Description
    protected
    AbstractDbTest(String schemaName, String dataSetResourceName)
    Creates a new test instance, using the given schema and reading the DBUnit XML file found in the given classpath resource.
    protected
    AbstractDbTest(String schemaName, String dataSetDtdResourceName, String... dataSetResourceNames)
    Creates a new test instance, using the given schema and reading the DBUnit XML files and DTD found in the given classpath resources.
  • Method Summary

    Modifier and Type
    Method
    Description
    protected final void
    Make finalize method final to avoid "Finalizer attacks" and corresponding SpotBugs warning (CT_CONSTRUCTOR_THROW).
    protected org.dbunit.dataset.IDataSet
    Gives the data set used by DbUnit when testing.
    protected String
    Gives the name of the resource containing the DTD for test data files.
    protected String[]
    Gives the names of the resources containing test data.
    protected abstract DataSource
    Override this to point to your datasource.
    protected Optional<org.dbunit.dataset.datatype.IDataTypeFactory>
    Gives the DbUnit IDataTypeFactory that matches the database used for testing, or null if the database type is left unspecified.
    protected String
    Gives the name of the database schema.
    protected org.dbunit.operation.DatabaseOperation
    Gives the operation to perform before executing each test.
    protected org.dbunit.operation.DatabaseOperation
    Gives the operation to perform after executing each test.
    void
    Called before each test case to insert test data into the database.
    void
    Called after each test case to clean the database from test data.

    Methods inherited from class java.lang.Object

    clone, equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • DEFAULT_SETUP_OPERATION

      public static final org.dbunit.operation.DatabaseOperation DEFAULT_SETUP_OPERATION
      The default operation to perform before executing each test.
    • DEFAULT_TEARDOWN_OPERATION

      public static final org.dbunit.operation.DatabaseOperation DEFAULT_TEARDOWN_OPERATION
      The default operation to perform after executing each test.
  • Constructor Details

    • AbstractDbTest

      protected AbstractDbTest(String schemaName, String dataSetResourceName)
      Creates a new test instance, using the given schema and reading the DBUnit XML file found in the given classpath resource.
      Parameters:
      schemaName - name of the database schema, or null
      dataSetResourceName - name of the classpath resource that contains DBUnit XML
    • AbstractDbTest

      protected AbstractDbTest(String schemaName, String dataSetDtdResourceName, String... dataSetResourceNames)
      Creates a new test instance, using the given schema and reading the DBUnit XML files and DTD found in the given classpath resources.
      Parameters:
      schemaName - name of the database schema, or null
      dataSetDtdResourceName - name of the classpath resource containing the DTD for the XML, or null to not validate
      dataSetResourceNames - names of classpath resources containing DBUnit XML
  • Method Details

    • setUpDatabase

      @BeforeEach public void setUpDatabase() throws Exception
      Called before each test case to insert test data into the database.
      Throws:
      Exception - if something goes wrong
    • tearDownDatabase

      @AfterEach public void tearDownDatabase() throws Exception
      Called after each test case to clean the database from test data.
      Throws:
      Exception - if something goes wrong
    • getSchemaName

      protected String getSchemaName()
      Gives the name of the database schema. Can be null.
      Returns:
      the database schema name
    • getDataSetDtdResourceName

      protected String getDataSetDtdResourceName()
      Gives the name of the resource containing the DTD for test data files.
      Returns:
      the name of the DTD resource
    • getDataSetResourceNames

      protected String[] getDataSetResourceNames()
      Gives the names of the resources containing test data.
      Returns:
      the names of the test data resources
    • getDataSet

      protected org.dbunit.dataset.IDataSet getDataSet() throws org.dbunit.dataset.DataSetException, IOException
      Gives the data set used by DbUnit when testing. The default implementation reads XML files from the classpath resources pointed to by the dataSetResourceNames property, validating using the DTD pointed to by the dataSetDtdResourceName property.

      Override this method if you want to provide the DbUnit test data in some other way.

      Returns:
      the test data set
      Throws:
      org.dbunit.dataset.DataSetException - if some resource if malformed
      IOException - if reading a resource failed
    • getDataSource

      protected abstract DataSource getDataSource()
      Override this to point to your datasource. The datasource could, e.g., be injected by Spring.
      Returns:
      the DataSource to use
    • getDataTypeFactory

      protected Optional<org.dbunit.dataset.datatype.IDataTypeFactory> getDataTypeFactory()
      Gives the DbUnit IDataTypeFactory that matches the database used for testing, or null if the database type is left unspecified. If left unspecified, DbUnit will probably issue a lot of warnings about "potential problems".

      The default implementation returns null. Override this to return the right IDataTypeFactory for your database, e.g., org.dbunit.ext.mysql.MySqlDataTypeFactory or org.dbunit.ext.oracle.Oracle10DataTypeFactory.

      To make it easy to change to use a different database, the factory could be configured by Spring and injected into your test case.

      Returns:
      theIDataTypeFactory to use
    • getSetUpOperation

      protected org.dbunit.operation.DatabaseOperation getSetUpOperation()
      Gives the operation to perform before executing each test.

      The default is DEFAULT_SETUP_OPERATION. Override this method to change this behavior.

      Returns:
      the setup operation to perform
    • getTearDownOperation

      protected org.dbunit.operation.DatabaseOperation getTearDownOperation()
      Gives the operation to perform after executing each test.

      The default is DEFAULT_TEARDOWN_OPERATION. Override this method to change this behavior.

      Returns:
      the setup operation to perform
    • finalize

      protected final void finalize() throws Throwable
      Make finalize method final to avoid "Finalizer attacks" and corresponding SpotBugs warning (CT_CONSTRUCTOR_THROW).
      Overrides:
      finalize in class Object
      Throws:
      Throwable
      See Also: