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

public final class DbTestHelper extends Object
A helper class used by AbstractDbTest. It could be used directly by a test class that for some reason cannot inherit from AbstractDbTest.
Author:
RealLifeDeveloper
  • Constructor Summary

    Constructors
    Constructor
    Description
    DbTestHelper(DataSource dataSource, org.dbunit.dataset.IDataSet dataSet, String schemaName, Optional<org.dbunit.dataset.datatype.IDataTypeFactory> dataTypeFactory)
    Creates a new DbTestHelper, with test data provided by the given dataSet and using the given dataSource to insert it.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Cleans the database from test data after each test case.
    void
    Initializes the test data before each test case.
    static org.dbunit.dataset.IDataSet
    readDataSetFromClasspath(String dataSetDtdResourceName, String... dataSetResourceNames)
    Reads XML data set files from the classpath resources pointed to by dataSetResourceNames, optionally validating using the DTD pointed to by dataSetDtdResourceName property.
    void
    setSetUpOperation(org.dbunit.operation.DatabaseOperation setUpOperation)
    Change the operation performed before executing each test.
    void
    setTearDownOperation(org.dbunit.operation.DatabaseOperation tearDownOperation)
    Change the operation performed after executing each test.

    Methods inherited from class java.lang.Object

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

    • DbTestHelper

      public DbTestHelper(DataSource dataSource, org.dbunit.dataset.IDataSet dataSet, String schemaName, Optional<org.dbunit.dataset.datatype.IDataTypeFactory> dataTypeFactory)
      Creates a new DbTestHelper, with test data provided by the given dataSet and using the given dataSource to insert it. The database schema name may be provided (can be null), and the type of database can be defined using dataTypeFactory (can also be null).
      Parameters:
      dataSource - the DataSource to use when inserting test data
      dataSet - the DbUnit test data set to read
      schemaName - the name of the database schema, or null
      dataTypeFactory - an IDataTypeFactory, or null
  • Method Details

    • readDataSetFromClasspath

      public static org.dbunit.dataset.IDataSet readDataSetFromClasspath(String dataSetDtdResourceName, String... dataSetResourceNames) throws org.dbunit.dataset.DataSetException, IOException
      Reads XML data set files from the classpath resources pointed to by dataSetResourceNames, optionally validating using the DTD pointed to by dataSetDtdResourceName property.

      The dataSetDtdResourceName parameter may be null, in which case no validation is performed.

      Parameters:
      dataSetDtdResourceName - the name of the resource containing the DTD for test data files, or null
      dataSetResourceNames - the names of the resources containing test data
      Returns:
      the test data set
      Throws:
      org.dbunit.dataset.DataSetException - if some resource if malformed
      IOException - if reading a resource failed
    • init

      public void init() throws Exception
      Initializes the test data before each test case.
      Throws:
      Exception - if something goes wrong
    • clean

      public void clean() throws Exception
      Cleans the database from test data after each test case.
      Throws:
      Exception - if something goes wrong
    • setSetUpOperation

      public void setSetUpOperation(org.dbunit.operation.DatabaseOperation setUpOperation)
      Change the operation performed before executing each test.

      The default setup operation is DatabaseOperation.CLEAN_INSERT.

      Parameters:
      setUpOperation - the new setup operation to use
    • setTearDownOperation

      public void setTearDownOperation(org.dbunit.operation.DatabaseOperation tearDownOperation)
      Change the operation performed after executing each test.

      The default is to perform no cleanup after the test.

      Parameters:
      tearDownOperation - the new teardown operation to use