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.DatabaseOperationThe default operation to perform before executing each test.static final org.dbunit.operation.DatabaseOperationThe default operation to perform after executing each test. -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedAbstractDbTest(@Nullable String schemaName, @Nullable 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.protectedAbstractDbTest(@Nullable 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. -
Method Summary
Modifier and TypeMethodDescriptionprotected final voidfinalize()Make finalize method final to avoid "Finalizer attacks" and corresponding SpotBugs warning (CT_CONSTRUCTOR_THROW).protected org.dbunit.dataset.IDataSetGives the data set used by DbUnit when testing.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 DataSourceOverride this to point to your datasource.protected Optional<org.dbunit.dataset.datatype.IDataTypeFactory>Gives the DbUnitIDataTypeFactorythat matches the database used for testing, ornullif the database type is left unspecified.Gives the name of the database schema.protected org.dbunit.operation.DatabaseOperationGives the operation to perform before executing each test.protected org.dbunit.operation.DatabaseOperationGives the operation to perform after executing each test.voidCalled before each test case to insert test data into the database.voidCalled 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, ornulldataSetResourceName- name of the classpath resource that contains DBUnit XML
-
AbstractDbTest
protected AbstractDbTest(@Nullable String schemaName, @Nullable 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, ornulldataSetDtdResourceName- name of the classpath resource containing the DTD for the XML, ornullto 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 thedataSetResourceNamesproperty, validating using the DTD pointed to by thedataSetDtdResourceNameproperty.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
DataSourceto use
-
getDataTypeFactory
Gives the DbUnitIDataTypeFactorythat matches the database used for testing, ornullif 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 rightIDataTypeFactoryfor your database, e.g.,org.dbunit.ext.mysql.MySqlDataTypeFactoryororg.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
IDataTypeFactoryto 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).
-