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 steps
} 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 steps
}
Note that it is unlikely that all test methods can share the same expected
data.
Release 2.5.2 introduced interface PrepAndExpectedTestCaseSteps and
the
runTest(VerifyTableDefinition[], String[], String[], PrepAndExpectedTestCaseSteps)
method. This allows for encapsulating test steps into an anonymous inner
class or a Java 8+ lambda and avoiding the try/catch/finally template in
tests.
@Autowired
private PrepAndExpectedTestCase tc;
@Test
public void testExample() throws Exception
{
final String[] prepDataFiles = {}; // define prep files
final String[] expectedDataFiles = {}; // define expected files
final VerifyTableDefinition[] tables = {}; // define tables to verify
final PrepAndExpectedTestCaseSteps testSteps = () -> {
// execute test steps
return null; // or an object for use outside the Steps
};
tc.runTest(tables, prepDataFiles, expectedDataFiles, testSteps);
}
org.dbunit.DefaultPrepAndExpectedTestCaseDiIT,
org.dbunit.DefaultPrepAndExpectedTestCaseExtIT| Modifier and Type | Field and Description |
|---|---|
static String |
TEST_ERROR_MSG |
| 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.
|
protected DatabaseOperation |
getSetUpOperation()
Returns the database operation executed in test setup.
|
VerifyTableDefinition[] |
getTableDefs()
Get the tableDefs.
|
protected DatabaseOperation |
getTearDownOperation()
Returns the database operation executed in test cleanup.
|
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().
|
Object |
runTest(VerifyTableDefinition[] verifyTables,
String[] prepDataFiles,
String[] expectedDataFiles,
PrepAndExpectedTestCaseSteps testSteps)
Run the DbUnit test.
|
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.
|
protected void |
tearDown() |
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, setUp, setUpDatabaseConfigassertEquals, 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 static final String TEST_ERROR_MSG
public 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 Object runTest(VerifyTableDefinition[] verifyTables, String[] prepDataFiles, String[] expectedDataFiles, PrepAndExpectedTestCaseSteps testSteps) throws Exception
runTest in interface PrepAndExpectedTestCaseverifyTables - 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.testSteps - The test steps to run.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 PrepAndExpectedTestCaseExceptionprotected void tearDown()
throws Exception
tearDown in class DatabaseTestCaseExceptionpublic void setupData()
throws Exception
IDatabaseTester.onSetup().Exceptionprotected DatabaseOperation getSetUpOperation() throws Exception
DatabaseTestCasegetSetUpOperation in class DatabaseTestCaseExceptionprotected DatabaseOperation getTearDownOperation() throws Exception
DatabaseTestCasegetTearDownOperation in class DatabaseTestCaseExceptionpublic 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-2017. All Rights Reserved.