Class DefaultPrepAndExpectedTestCase
- All Implemented Interfaces:
PrepAndExpectedTestCase,org.junit.jupiter.api.extension.Extension,org.junit.jupiter.api.extension.InvocationInterceptor,org.junit.jupiter.api.extension.TestInstantiationAwareExtension
configureTest(), setupData(), verifyData(), and cleanupData() share one
IDatabaseConnection for a test's lifecycle,
acquired lazily on first use and closed once by cleanupData(), instead of
each acquiring (and often closing) its own. Calling any of those methods
without an eventual cleanupData() call - e.g. testing them individually
rather than through preTest()/postTest() - leaves that connection open.
If databaseTester is configured with a
CachingConnectionProvider shared across test
methods, set setCloseConnectionAfterTest(boolean) to false so
cleanupData() does not close a connection other tests still expect to
reuse; the provider's owner is then responsible for closing it once,
itself, when the whole run finishes.
The verifyData() method hands assertion failures to
DefaultFailureHandler by default, which throws
on the first mismatch found. Set setFailureHandler(FailureHandler)
to, for example, a DiffCollectingFailureHandler
to collect every Difference instead; that is
not the default since most tests want to keep failing fast.
- Since:
- 2.4.8
- Version:
- $Revision$ $Date$
- Author:
- Jeff Jensen jeffjensen AT users.sourceforge.net, Last changed by: $Author$
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.InvocationInterceptor
org.junit.jupiter.api.extension.InvocationInterceptor.Invocation<T>Nested classes/interfaces inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
org.junit.jupiter.api.extension.TestInstantiationAwareExtension.ExtensionContextScope -
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final StringMessage prefix used for wrapped test failures. -
Constructor Summary
ConstructorsConstructorDescriptionCreate new instance.Create new instance with specified test case name.DefaultPrepAndExpectedTestCase(DataFileLoader dataFileLoader, IDatabaseTester databaseTester) Create new instance with specified dataFileLoader and databaseTester.DefaultPrepAndExpectedTestCase(DataFileLoader dataFileLoader, IDatabaseTester databaseTester, boolean closeConnectionAfterTest) Create new instance with specified dataFileLoader and databaseTester. -
Method Summary
Modifier and TypeMethodDescriptionapplyColumnFilters(ITable table, String[] excludeColumns, String[] includeColumns) Apply the specified exclude and include column filters to the specified table.voidCleanup tables specified in prep and expected datasets, using the provided databaseTester.protected voidcompareData(ITable expectedTable, ITable actualTable, Column[] additionalColumnInfo, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers) Compare the tables, enables easy overriding.voidconfigureTest(VerifyTableDefinition[] verifyTableDefinitions, String[] prepDataFiles, String[] expectedDataFiles) Configure the test.Get the databaseTester.Get the dataFileLoader.Returns the test dataset.Get the expected dataset, created from the expectedDataFiles.Returns the verifier used to check that verify table definitions and the expected dataset agree.Get the failureHandler.Get the prep dataset, created from the prepDataFiles.protected DatabaseOperationReturns the database operation executed in test setup.protected DatabaseOperationReturns the database operation executed in test cleanup.Get the tableDefs.booleanGet whether the connection lookupFeatureValue() and cleanupData() are done with is closed.loadTableDataFromDatabase(String tableName, IDatabaseConnection connection) Loads the given table's actual data from the database.loadTableDataFromDataSet(String tableName) Loads the given table's expected data from the expected dataset.protected Column[]makeAdditionalColumnInfo(String[] excludeColumns, Column[] allColumns) Don't add excluded columns to additionalColumnInfo as they are not found and generate a not found message in the fail message.protected Column[]makeAdditionalColumnInfo(ITable expectedTable, String[] excludeColumns) Don't add excluded columns to additionalColumnInfo as they are not found and generate a not found message in the fail message.makeCompositeDataSet(String[] dataFiles, String dataFilesName) Make aIDataSetfrom the specified files with case sensitive table names as false.makeCompositeDataSet(String[] dataFiles, String dataFilesName, boolean isCaseSensitiveTableNames) Make aIDataSetfrom the specified files.Creates a new IDatabaseTester.voidpostTest()Execute all post-test steps.voidpostTest(boolean verifyData) Execute post-test steps.voidpreTest()Execute pre-test steps.voidpreTest(VerifyTableDefinition[] tables, String[] prepDataFiles, String[] expectedDataFiles) Convenience method to call configureTest() and preTest().runTest(VerifyTableDefinition[] verifyTables, String[] prepDataFiles, String[] expectedDataFiles, PrepAndExpectedTestCaseSteps testSteps) Run the DbUnit test.protected ObjectrunTestSteps(PrepAndExpectedTestCaseSteps testSteps) Run the provided test steps.voidsetCloseConnectionAfterTest(boolean closeConnectionAfterTest) Set whether the connection lookupFeatureValue() and cleanupData() are done with is closed.voidsetDatabaseTester(IDatabaseTester databaseTester) Set the databaseTester.voidsetDataFileLoader(DataFileLoader dataFileLoader) Set the dataFileLoader.voidsetExpectedDataSetAndVerifyTableDefinitionVerifier(ExpectedDataSetAndVerifyTableDefinitionVerifier expectedDataSetAndVerifyTableDefinitionVerifier) Sets the verifier used to check that verify table definitions and the expected dataset agree.voidsetExpectedDs(IDataSet expectedDataSet) Set the expectedDs.voidsetFailureHandler(FailureHandler failureHandler) Set the failureHandler.voidSet the prepDs.voidUse the provided databaseTester to prep the database with the provided prep dataset.voidsetVerifyTableDefs(VerifyTableDefinition[] verifyTableDefs) Set the tableDefs.protected voidtearDown()Legacy JUnit-3-era tear-down hook.voidFor the provided VerifyTableDefinitions, verify each table's actual results are as expected.protected voidverifyData(IDatabaseConnection connection, VerifyTableDefinition verifyTableDefinition) Verifies a single table's actual data against its expected data.protected voidverifyData(ITable expectedTable, ITable actualTable, String[] excludeColumns, String[] includeColumns, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers) For the specified expected and actual tables (and excluding and including the specified columns), verify the actual data is as expected.protected voidverifyData(ITable expectedTable, ITable actualTable, String[] excludeColumns, String[] includeColumns, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers, boolean sortOnFilteredColumnsOnly) For the specified expected and actual tables (and excluding and including the specified columns), verify the actual data is as expected.Methods inherited from class org.dbunit.DBTestCase
getConnectionMethods inherited from class org.dbunit.DatabaseTestCase
closeConnection, getName, getOperationListener, setUp, setUpDatabaseConfig, tearDownMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitMethods inherited from interface org.junit.jupiter.api.extension.InvocationInterceptor
interceptAfterAllMethod, interceptAfterEachMethod, interceptBeforeAllMethod, interceptBeforeEachMethod, interceptDynamicTest, interceptTestClassConstructor, interceptTestFactoryMethod, interceptTestMethod, interceptTestTemplateMethodMethods inherited from interface org.junit.jupiter.api.extension.TestInstantiationAwareExtension
getTestInstantiationExtensionContextScope
-
Field Details
-
TEST_ERROR_MSG
Message prefix used for wrapped test failures.- See Also:
-
-
Constructor Details
-
DefaultPrepAndExpectedTestCase
public DefaultPrepAndExpectedTestCase()Create new instance. -
DefaultPrepAndExpectedTestCase
public DefaultPrepAndExpectedTestCase(DataFileLoader dataFileLoader, IDatabaseTester databaseTester) Create new instance with specified dataFileLoader and databaseTester.- Parameters:
dataFileLoader- Load to use for loading the data files.databaseTester- Tester to use for database manipulation.
-
DefaultPrepAndExpectedTestCase
public DefaultPrepAndExpectedTestCase(DataFileLoader dataFileLoader, IDatabaseTester databaseTester, boolean closeConnectionAfterTest) Create new instance with specified dataFileLoader and databaseTester.- Parameters:
dataFileLoader- Load to use for loading the data files.databaseTester- Tester to use for database manipulation.closeConnectionAfterTest- Whether or not to close the database connection after each test.- Since:
- 3.4.0
-
DefaultPrepAndExpectedTestCase
Create new instance with specified test case name.- Parameters:
name- The test case name.
-
-
Method Details
-
newDatabaseTester
Creates a new IDatabaseTester. Default implementation returns aPropertiesBasedJdbcDatabaseTester. This implementation returns the databaseTester set by the test.- Overrides:
newDatabaseTesterin classDBTestCase- Returns:
- the newly created database tester.
- Throws:
Exception- if the database tester cannot be created.
-
getDataSet
Returns the test dataset. Returns the prep dataset.- Specified by:
getDataSetin classDatabaseTestCase- Returns:
- the test dataset.
- Throws:
Exception- if the dataset cannot be retrieved or created.
-
configureTest
public void configureTest(VerifyTableDefinition[] verifyTableDefinitions, String[] prepDataFiles, String[] expectedDataFiles) throws Exception Configure the test. Call this method before performing the test steps.Executes against the connection shared with setupData(), verifyData() and cleanupData() for this test's lifecycle rather than a fresh one, and leaves it open; cleanupData() closes it. See #800.
- Specified by:
configureTestin interfacePrepAndExpectedTestCase- Parameters:
verifyTableDefinitions- Table definitions to verify after test execution.prepDataFiles- The prep data files (as classpath resources) to load and insert contents into the database as seed data.expectedDataFiles- The expected data files (as classpath resources) to load as expected data and verify actual data matches at test end.- Throws:
Exception- if the test cannot be configured.
-
preTest
Execute pre-test steps. Call this method before performing the test steps.- Specified by:
preTestin interfacePrepAndExpectedTestCase- Throws:
Exception- if the pre-test steps fail.
-
preTest
public void preTest(VerifyTableDefinition[] tables, String[] prepDataFiles, String[] expectedDataFiles) throws Exception Convenience method to call configureTest() and preTest().- Specified by:
preTestin interfacePrepAndExpectedTestCase- Parameters:
tables- Table definitions to verify after test execution.prepDataFiles- The prep data files (as classpath resources) to load and insert contents into the database as seed data.expectedDataFiles- The expected data files (as classpath resources) to load as expected data and verify actual data matches at test end.- Throws:
Exception- if the pre-test steps fail.
-
runTest
public Object runTest(VerifyTableDefinition[] verifyTables, String[] prepDataFiles, String[] expectedDataFiles, PrepAndExpectedTestCaseSteps testSteps) throws Exception Run the DbUnit test.- Specified by:
runTestin interfacePrepAndExpectedTestCase- Parameters:
verifyTables- Table definitions to verify after test execution.prepDataFiles- The prep data files (as classpath resources) to load and insert contents into the database as seed data.expectedDataFiles- The expected data files (as classpath resources) to load as expected data and verify actual data matches at test end.testSteps- The test steps to run.- Returns:
- User defined object from running the test steps.
- Throws:
Exception- if the test steps fail.
-
runTestSteps
Run the provided test steps. Override as necessary for custom logic.- Parameters:
testSteps- The test steps to run.- Returns:
- the user-defined object returned by the test steps.
- Throws:
Exception- if the test steps fail.
-
postTest
Execute all post-test steps. Call this method after performing the test steps.- Specified by:
postTestin interfacePrepAndExpectedTestCase- Throws:
Exception- if the post-test steps fail.
-
postTest
Execute post-test steps. Call this method after performing the test steps.- Specified by:
postTestin interfacePrepAndExpectedTestCase- Parameters:
verifyData- 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.- Throws:
Exception- if the post-test steps fail.
-
cleanupData
Cleanup tables specified in prep and expected datasets, using the provided databaseTester. SeeIDatabaseTester.onTearDown().Runs the tear down operation against the connection shared with setupData() and verifyData() for this test's lifecycle, then closes it. See #800.
- Specified by:
cleanupDatain interfacePrepAndExpectedTestCase- Throws:
Exception- if cleaning up the data fails.
-
tearDown
Legacy JUnit-3-era tear-down hook. Not invoked automatically under JUnit 5; kept for subclasses that drive the lifecycle manually. Calling it after a fullrunTest(org.dbunit.VerifyTableDefinition[], java.lang.String[], java.lang.String[], org.dbunit.PrepAndExpectedTestCaseSteps)orpostTest()cycle cleans up a second time (parent tearDown() re-runs the tear down operation on the prep dataset with a fresh connection).- Overrides:
tearDownin classDatabaseTestCase- Throws:
Exception- if the tear-down operation fails.
-
setupData
Use the provided databaseTester to prep the database with the provided prep dataset. SeeIDatabaseTester.onSetup().Executes against the connection shared with verifyData() and cleanupData() for this test's lifecycle rather than a fresh one, and leaves it open; cleanupData() closes it. See #800.
- Throws:
Exception- if preparing the data fails.
-
getSetUpOperation
Description copied from class:DatabaseTestCaseReturns the database operation executed in test setup.- Overrides:
getSetUpOperationin classDatabaseTestCase- Returns:
- the database operation executed in test setup.
- Throws:
Exception- if the operation cannot be determined.
-
getTearDownOperation
Description copied from class:DatabaseTestCaseReturns the database operation executed in test cleanup.- Overrides:
getTearDownOperationin classDatabaseTestCase- Returns:
- the database operation executed in test cleanup.
- Throws:
Exception- if the operation cannot be determined.
-
verifyData
For the provided VerifyTableDefinitions, verify each table's actual results are as expected. Uses the connection from the provided databaseTester, shared with setupData() and cleanupData() for this test's lifecycle. Left open on return; cleanupData() closes it. See #800.- Specified by:
verifyDatain interfacePrepAndExpectedTestCase- Throws:
Exception- if verifying the data fails.
-
verifyData
protected void verifyData(IDatabaseConnection connection, VerifyTableDefinition verifyTableDefinition) throws Exception Verifies a single table's actual data against its expected data.- Parameters:
connection- the database connection to load the actual data from.verifyTableDefinition- the table definition to verify.- Throws:
Exception- if verifying the table fails.
-
loadTableDataFromDataSet
Loads the given table's expected data from the expected dataset.- Parameters:
tableName- the name of the table to load.- Returns:
- the table's expected data.
- Throws:
DataSetException- if loading the table fails.
-
loadTableDataFromDatabase
public ITable loadTableDataFromDatabase(String tableName, IDatabaseConnection connection) throws Exception Loads the given table's actual data from the database.- Parameters:
tableName- the name of the table to load.connection- the database connection to load the data from.- Returns:
- the table's actual data.
- Throws:
Exception- if loading the table fails.
-
verifyData
protected void verifyData(ITable expectedTable, ITable actualTable, String[] excludeColumns, String[] includeColumns, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers) throws DatabaseUnitExceptionFor the specified expected and actual tables (and excluding and including the specified columns), verify the actual data is as expected.- Parameters:
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. SeeDefaultColumnFilter.excludeColumn(String).includeColumns- The column names to only include in comparison. SeeDefaultColumnFilter.includeColumn(String).defaultValueComparer-ValueComparerto use with column value comparisons when the column name for the table is not in the columnValueComparersMap. Can benulland will default.columnValueComparers-MapofValueComparers to use for specific columns. Key is column name, value is theValueComparer. Can benulland will default to defaultValueComparer for all columns in all tables.- Throws:
DatabaseUnitException- if the tables' row counts, columns, or data do not match.- See Also:
-
verifyData
protected void verifyData(ITable expectedTable, ITable actualTable, String[] excludeColumns, String[] includeColumns, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers, boolean sortOnFilteredColumnsOnly) throws DatabaseUnitExceptionFor the specified expected and actual tables (and excluding and including the specified columns), verify the actual data is as expected.- Parameters:
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. SeeDefaultColumnFilter.excludeColumn(String).includeColumns- The column names to only include in comparison. SeeDefaultColumnFilter.includeColumn(String).defaultValueComparer-ValueComparerto use with column value comparisons when the column name for the table is not in the columnValueComparersMap. Can benulland will default.columnValueComparers-MapofValueComparers to use for specific columns. Key is column name, value is theValueComparer. Can benulland will default to defaultValueComparer for all columns in all tables.sortOnFilteredColumnsOnly- True to sort the expected and actual tables by only the columns that survive excludeColumns/includeColumns, instead of by all of the actual table's native columns; seeVerifyTableDefinition.isSortOnFilteredColumnsOnly().- Throws:
DatabaseUnitException- if the tables' row counts, columns, or data do not match.- Since:
- 3.5.0
-
compareData
protected void compareData(ITable expectedTable, ITable actualTable, Column[] additionalColumnInfo, ValueComparer defaultValueComparer, Map<String, ValueComparer> columnValueComparers) throws DatabaseUnitExceptionCompare the tables, enables easy overriding.Uses
failureHandlerwhen set; otherwise defers toAssertion.assertWithValueComparer(ITable, ITable, Column[], ValueComparer, Map)'s ownDefaultFailureHandler, configured with additionalColumnInfo.- Parameters:
expectedTable- the table containing all expected results.actualTable- the table containing all actual results.additionalColumnInfo- the additional columns to include in failure messages.defaultValueComparer- the value comparer used when no more specific comparer is configured.columnValueComparers- the per-column value comparers to use.- Throws:
DatabaseUnitException- if the tables' row counts, columns, or data do not match.
-
makeAdditionalColumnInfo
protected Column[] makeAdditionalColumnInfo(ITable expectedTable, String[] excludeColumns) throws DataSetException Don't add excluded columns to additionalColumnInfo as they are not found and generate a not found message in the fail message.- Parameters:
expectedTable- Not null.excludeColumns- Nullable.- Returns:
- the additional column info, excluding excludeColumns.
- Throws:
DataSetException- if the expected table's columns cannot be retrieved.
-
makeAdditionalColumnInfo
Don't add excluded columns to additionalColumnInfo as they are not found and generate a not found message in the fail message.- Parameters:
excludeColumns- Not null.allColumns- Not null.- Returns:
- the additional column info, excluding excludeColumns.
-
makeCompositeDataSet
public IDataSet makeCompositeDataSet(String[] dataFiles, String dataFilesName) throws DataSetException Make aIDataSetfrom the specified files with case sensitive table names as false.- Parameters:
dataFiles- Represents the array of dbUnit data files.dataFilesName- Concept name of the data files, e.g. prep, expected.- Returns:
- The composite dataset.
- Throws:
DataSetException- On dbUnit errors.
-
makeCompositeDataSet
public IDataSet makeCompositeDataSet(String[] dataFiles, String dataFilesName, boolean isCaseSensitiveTableNames) throws DataSetException Make aIDataSetfrom the specified files.- Parameters:
dataFiles- Represents the array of dbUnit data files.dataFilesName- Concept name of the data files, e.g. prep, expected.isCaseSensitiveTableNames- true if case sensitive table names is on.- Returns:
- The composite dataset.
- Throws:
DataSetException- On dbUnit errors.
-
applyColumnFilters
public ITable applyColumnFilters(ITable table, String[] excludeColumns, String[] includeColumns) throws DataSetException Apply the specified exclude and include column filters to the specified table.- Parameters:
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.- Returns:
- The filtered table.
- Throws:
DataSetException- if applying the filters fails.
-
getPrepDataset
Get the prep dataset, created from the prepDataFiles.- Specified by:
getPrepDatasetin interfacePrepAndExpectedTestCase- Returns:
- The prep dataset.
-
getExpectedDataset
Get the expected dataset, created from the expectedDataFiles.- Specified by:
getExpectedDatasetin interfacePrepAndExpectedTestCase- Returns:
- The expected dataset.
-
getDatabaseTester
Get the databaseTester.- Overrides:
getDatabaseTesterin classDatabaseTestCase- Returns:
- The databaseTester.
- See Also:
-
setDatabaseTester
Set the databaseTester.- Parameters:
databaseTester- The databaseTester to set.- See Also:
-
isCloseConnectionAfterTest
public boolean isCloseConnectionAfterTest()Get whether the connection lookupFeatureValue() and cleanupData() are done with is closed.- Returns:
- True if it is closed, false if not.
- Since:
- 3.4.0
- See Also:
-
setCloseConnectionAfterTest
public void setCloseConnectionAfterTest(boolean closeConnectionAfterTest) Set whether the connection lookupFeatureValue() and cleanupData() are done with is closed. Default is true. Set to false when databaseTester shares aCachingConnectionProvideracross test methods, so this instance does not close a connection other tests still expect to reuse.- Parameters:
closeConnectionAfterTest- True to close it, false to leave it open.- Since:
- 3.4.0
- See Also:
-
getDataFileLoader
Get the dataFileLoader.- Returns:
- The dataFileLoader.
- See Also:
-
setDataFileLoader
Set the dataFileLoader.- Parameters:
dataFileLoader- The dataFileLoader to set.- See Also:
-
setPrepDs
Set the prepDs.- Parameters:
prepDataSet- The prepDs to set.- See Also:
-
setExpectedDs
Set the expectedDs.- Parameters:
expectedDataSet- The expectedDs to set.- See Also:
-
getVerifyTableDefs
Get the tableDefs.- Returns:
- The tableDefs.
- See Also:
-
setVerifyTableDefs
Set the tableDefs.- Parameters:
verifyTableDefs- The tableDefs to set.- See Also:
-
getExpectedDataSetAndVerifyTableDefinitionVerifier
public ExpectedDataSetAndVerifyTableDefinitionVerifier getExpectedDataSetAndVerifyTableDefinitionVerifier()Returns the verifier used to check that verify table definitions and the expected dataset agree.- Returns:
- the verifier used to check that verify table definitions and the expected dataset agree.
-
setExpectedDataSetAndVerifyTableDefinitionVerifier
public void setExpectedDataSetAndVerifyTableDefinitionVerifier(ExpectedDataSetAndVerifyTableDefinitionVerifier expectedDataSetAndVerifyTableDefinitionVerifier) Sets the verifier used to check that verify table definitions and the expected dataset agree.- Parameters:
expectedDataSetAndVerifyTableDefinitionVerifier- the verifier to use.
-
getFailureHandler
Get the failureHandler.- Returns:
- The failureHandler.
- Since:
- 3.5.0
- See Also:
-
setFailureHandler
Set the failureHandler.- Parameters:
failureHandler- The failureHandler to set.- Since:
- 3.5.0
- See Also:
-