Class DbTestHelper
java.lang.Object
com.reallifedeveloper.tools.test.database.dbunit.DbTestHelper
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
ConstructorsConstructorDescriptionDbTestHelper
(DataSource dataSource, org.dbunit.dataset.IDataSet dataSet, String schemaName, Optional<org.dbunit.dataset.datatype.IDataTypeFactory> dataTypeFactory) Creates a newDbTestHelper
, with test data provided by the givendataSet
and using the givendataSource
to insert it. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clean()
Cleans the database from test data after each test case.void
init()
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 bydataSetResourceNames
, optionally validating using the DTD pointed to bydataSetDtdResourceName
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.
-
Constructor Details
-
DbTestHelper
public DbTestHelper(DataSource dataSource, org.dbunit.dataset.IDataSet dataSet, String schemaName, Optional<org.dbunit.dataset.datatype.IDataTypeFactory> dataTypeFactory) Creates a newDbTestHelper
, with test data provided by the givendataSet
and using the givendataSource
to insert it. The database schema name may be provided (can benull
), and the type of database can be defined usingdataTypeFactory
(can also benull
).- Parameters:
dataSource
- theDataSource
to use when inserting test datadataSet
- the DbUnit test data set to readschemaName
- the name of the database schema, ornull
dataTypeFactory
- anIDataTypeFactory
, ornull
-
-
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 bydataSetResourceNames
, optionally validating using the DTD pointed to bydataSetDtdResourceName
property.The
dataSetDtdResourceName
parameter may benull
, in which case no validation is performed.- Parameters:
dataSetDtdResourceName
- the name of the resource containing the DTD for test data files, ornull
dataSetResourceNames
- the names of the resources containing test data- Returns:
- the test data set
- Throws:
org.dbunit.dataset.DataSetException
- if some resource if malformedIOException
- if reading a resource failed
-
init
Initializes the test data before each test case.- Throws:
Exception
- if something goes wrong
-
clean
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
-