Class AbstractDbTest
java.lang.Object
com.reallifedeveloper.tools.test.database.dbunit.AbstractDbTest
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
FieldsModifier and TypeFieldDescriptionstatic 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
ConstructorsModifierConstructorDescriptionprotected
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 TypeMethodDescriptionprotected final void
finalize()
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 DbUnitIDataTypeFactory
that matches the database used for testing, ornull
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.
-
Field Details
-
DEFAULT_SETUP_OPERATION
public static final org.dbunit.operation.DatabaseOperation DEFAULT_SETUP_OPERATIONThe default operation to perform before executing each test. -
DEFAULT_TEARDOWN_OPERATION
public static final org.dbunit.operation.DatabaseOperation DEFAULT_TEARDOWN_OPERATIONThe default operation to perform after executing each test.
-
-
Constructor Details
-
AbstractDbTest
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, ornull
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, ornull
dataSetDtdResourceName
- name of the classpath resource containing the DTD for the XML, ornull
to not validatedataSetResourceNames
- names of classpath resources containing DBUnit XML
-
-
Method Details
-
setUpDatabase
Called before each test case to insert test data into the database.- Throws:
Exception
- if something goes wrong
-
tearDownDatabase
Called after each test case to clean the database from test data.- Throws:
Exception
- if something goes wrong
-
getSchemaName
Gives the name of the database schema. Can benull
.- Returns:
- the database schema name
-
getDataSetDtdResourceName
Gives the name of the resource containing the DTD for test data files.- Returns:
- the name of the DTD resource
-
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, IOExceptionGives the data set used by DbUnit when testing. The default implementation reads XML files from the classpath resources pointed to by thedataSetResourceNames
property, validating using the DTD pointed to by thedataSetDtdResourceName
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 malformedIOException
- if reading a resource failed
-
getDataSource
Override this to point to your datasource. The datasource could, e.g., be injected by Spring.- Returns:
- the
DataSource
to use
-
getDataTypeFactory
Gives the DbUnitIDataTypeFactory
that matches the database used for testing, ornull
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 rightIDataTypeFactory
for your database, e.g.,org.dbunit.ext.mysql.MySqlDataTypeFactory
ororg.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:
- the
IDataTypeFactory
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
Make finalize method final to avoid "Finalizer attacks" and corresponding SpotBugs warning (CT_CONSTRUCTOR_THROW).
-