public class DefaultPrepAndExpectedTestCase extends DBTestCase implements PrepAndExpectedTestCase
Configure a bean of its interface, injecting a IDatabaseTester and a DataFileLoader using the databaseTester and a dataFileLoader properties.
Obtain IDatabaseTester and DataFileLoader instances (possibly dependency injecting them into the test class) and set them accordingly, probably in a setup type of method, such as:
@Before
public void setDbunitTestDependencies() {
setDatabaseTester(databaseTester);
setDataFileLoader(dataFileLoader);
}
@Autowired
private PrepAndExpectedTestCase tc;
@Test
public void testExample() throws Exception {
try {
final String[] prepDataFiles = {}; // define prep files
final String[] expectedDataFiles = {}; // define expected files
final VerifyTableDefinition[] tables = {}; // define tables to verify
tc.preTest(tables, prepDataFiles, expectedDataFiles);
// execute test
} catch (Exception e) {
log.error("Test error", e);
throw e;
} finally {
tc.postTest();
}
}
@Override
protected void setUp() throws Exception {
setDatabaseTester(databaseTester);
setDataFileLoader(dataFileLoader);
String[] prepDataFiles = {}; //define prep files
String[] expectedDataFiles = {}; // define expected files
VerifyTableDefinition[] tables = {}; //define tables to verify
preTest(tables, prepDataFiles, expectedDataFiles);
// call this if overriding setUp() and databaseTester & dataFileLoader are already set.
super.setUp();
}
@Override
protected void tearDown() throws Exception {
postTest();
super.tearDown();
}
@Test
public void testExample() throws Exception {
// execute test
}
Note that it is unlikely that all test methods can share the same expected
data.
org.dbunit.DefaultPrepAndExpectedTestCaseDiIT,
org.dbunit.DefaultPrepAndExpectedTestCaseExtIT| Constructor and Description |
|---|
DefaultPrepAndExpectedTestCase()
Create new instance.
|
DefaultPrepAndExpectedTestCase(DataFileLoader dataFileLoader,
IDatabaseTester databaseTester)
Create new instance with specified dataFileLoader and databasetester.
|
DefaultPrepAndExpectedTestCase(String name)
Create new instance with specified test case name.
|
| Modifier and Type | Method and Description |
|---|---|
ITable |
applyColumnFilters(ITable table,
String[] excludeColumns,
String[] includeColumns)
Apply the specified exclude and include column filters to the specified
table.
|
void |
cleanupData()
Cleanup tables specified in prep and expected datasets, using the
provided databaseTester.
|
void |
configureTest(VerifyTableDefinition[] tables,
String[] prepDataFiles,
String[] expectedDataFiles)
Configure the test.
|
IDatabaseTester |
getDatabaseTester()
Get the databaseTester.
|
DataFileLoader |
getDataFileLoader()
Get the dataFileLoader.
|
IDataSet |
getDataSet()
Returns the test dataset.
|
IDataSet |
getExpectedDataset()
Get the expected dataset, created from the expectedDataFiles.
|
IDataSet |
getPrepDataset()
Get the prep dataset, created from the prepDataFiles.
|
VerifyTableDefinition[] |
getTableDefs()
Get the tableDefs.
|
IDataSet |
makeCompositeDataSet(String[] dataFiles)
Make a
IDataSet from the specified files. |
IDatabaseTester |
newDatabaseTester()
Creates a new IDatabaseTester.
|
void |
postTest()
Execute all post-test steps.
|
void |
postTest(boolean verifyData)
Execute post-test steps.
|
void |
preTest()
Execute pre-test steps.
|
void |
preTest(VerifyTableDefinition[] tables,
String[] prepDataFiles,
String[] expectedDataFiles)
Convenience method to call configureTest() and preTest().
|
void |
setDatabaseTester(IDatabaseTester databaseTester)
Set the databaseTester.
|
void |
setDataFileLoader(DataFileLoader dataFileLoader)
Set the dataFileLoader.
|
void |
setExpectedDs(IDataSet expectedDs)
Set the expectedDs.
|
void |
setPrepDs(IDataSet prepDs)
Set the prepDs.
|
void |
setTableDefs(VerifyTableDefinition[] tableDefs)
Set the tableDefs.
|
void |
setupData()
Use the provided databaseTester to prep the database with the provided
prep dataset.
|
void |
verifyData()
For the provided VerifyTableDefinitions, verify each table's actual
results are as expected.
|
void |
verifyData(ITable expectedTable,
ITable actualTable,
String[] excludeColumns,
String[] includeColumns)
For the specified expected and actual tables (and excluding and including
the specified columns), verify the actual data is as expected.
|
getConnectioncloseConnection, getOperationListener, getSetUpOperation, getTearDownOperation, setUp, setUpDatabaseConfig, tearDownassertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, getName, run, run, runBare, runTest, setName, toStringpublic DefaultPrepAndExpectedTestCase()
public DefaultPrepAndExpectedTestCase(DataFileLoader dataFileLoader, IDatabaseTester databaseTester)
dataFileLoader - Load to use for loading the data files.databaseTester - Tester to use for database manipulation.public DefaultPrepAndExpectedTestCase(String name)
name - The test case name.public IDatabaseTester newDatabaseTester() throws Exception
PropertiesBasedJdbcDatabaseTester. This implementation returns the databaseTester set by the
test.newDatabaseTester in class DBTestCaseExceptionpublic IDataSet getDataSet() throws Exception
getDataSet in class DatabaseTestCaseExceptionpublic void configureTest(VerifyTableDefinition[] tables, String[] prepDataFiles, String[] expectedDataFiles) throws Exception
configureTest in interface PrepAndExpectedTestCasetables - Table definitions to verify after test execution.prepDataFiles - The prep data files to load as seed data.expectedDataFiles - The expected data files to load as expected data.Exceptionpublic void preTest()
throws Exception
preTest in interface PrepAndExpectedTestCaseExceptionpublic void preTest(VerifyTableDefinition[] tables, String[] prepDataFiles, String[] expectedDataFiles) throws Exception
preTest in interface PrepAndExpectedTestCasetables - Table definitions to verify after test execution.prepDataFiles - The prep data files to load as seed data.expectedDataFiles - The expected data files to load as expected data.Exceptionpublic void postTest()
throws Exception
postTest in interface PrepAndExpectedTestCaseExceptionpublic void postTest(boolean verifyData)
throws Exception
postTest in interface PrepAndExpectedTestCaseverifyData - Specify true to perform verify data steps, false to not.
Useful to specify false when test has failure in progress
(e.g. an exception) and verifying data would fail, masking
original test failure.Exceptionpublic void cleanupData()
throws Exception
IDatabaseTester.onTearDown().cleanupData in interface PrepAndExpectedTestCaseExceptionpublic void setupData()
throws Exception
IDatabaseTester.onSetup().Exceptionpublic void verifyData()
throws Exception
verifyData in interface PrepAndExpectedTestCaseExceptionpublic void verifyData(ITable expectedTable, ITable actualTable, String[] excludeColumns, String[] includeColumns) throws DatabaseUnitException
expectedTable - The expected table to compare the actual table to.actualTable - The actual table to compare to the expected table.excludeColumns - The column names to exclude from comparison. See
DefaultColumnFilter.excludeColumn(String)
.includeColumns - The column names to only include in comparison. See
DefaultColumnFilter.includeColumn(String)
.DatabaseUnitExceptionpublic IDataSet makeCompositeDataSet(String[] dataFiles) throws DataSetException
IDataSet from the specified files.dataFiles - Represents the array of dbUnit data files.DataSetException - On dbUnit errors.public ITable applyColumnFilters(ITable table, String[] excludeColumns, String[] includeColumns) throws DataSetException
table - The table to apply the filters to.excludeColumns - The exclude filters; use null or empty array to mean exclude
none.includeColumns - The include filters; use null to mean include all.DataSetExceptionpublic IDataSet getPrepDataset()
getPrepDataset in interface PrepAndExpectedTestCasepublic IDataSet getExpectedDataset()
getExpectedDataset in interface PrepAndExpectedTestCasepublic IDatabaseTester getDatabaseTester()
getDatabaseTester in class DatabaseTestCasedatabaseTester}.public void setDatabaseTester(IDatabaseTester databaseTester)
databaseTester - The databaseTester to set.databaseTester}.public DataFileLoader getDataFileLoader()
dataFileLoader}.public void setDataFileLoader(DataFileLoader dataFileLoader)
dataFileLoader - The dataFileLoader to set.dataFileLoader}.public void setPrepDs(IDataSet prepDs)
prepDs - The prepDs to set.prepDs}.public void setExpectedDs(IDataSet expectedDs)
expectedDs - The expectedDs to set.expectedDs}.public VerifyTableDefinition[] getTableDefs()
tableDefs}.public void setTableDefs(VerifyTableDefinition[] tableDefs)
tableDefs - The tableDefs to set.tableDefs}.Copyright © 2002-2015. All Rights Reserved.