org.dbunit
Class DefaultPrepAndExpectedTestCase

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by org.dbunit.DatabaseTestCase
              extended by org.dbunit.DBTestCase
                  extended by org.dbunit.DefaultPrepAndExpectedTestCase
All Implemented Interfaces:
junit.framework.Test, PrepAndExpectedTestCase

public class DefaultPrepAndExpectedTestCase
extends DBTestCase
implements PrepAndExpectedTestCase

Test case base class supporting prep data and expected data. Prep data is the data needed for the test to run. Expected data is the data needed to compare if the test ran successfully. Use this class in two ways:

  1. Dependency inject it as its interface into a test class.
  2. Configure a bean of its interface, injecting a IDatabaseTester and a DataFileLoader using the databaseTester and a dataFileLoader properties.

  3. Extend it in a test class.
  4. 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);
     }
     

To setup, execute, and clean up tests, call the configureTest(), preTest(), and postTest() methods. Note there is a preTest() convenience method that takes the same parameters as the configureTest() method; use it instead of using both configureTest() and preTest(). Where the test case calls them depends on data needs:

When each test method requires different prep and expected data

If each test method requires its own prep and expected data, then the test methods will look something like the following:
 @Autowired
 private PrepAndExpectedTestCase tc;
 
 @Test
 public void testExample() throws Exception {
     String[] prepDataFiles = {}; // define prep files
     String[] expectedDataFiles = {}; // define expected files
     VerifyTableDefinition[] tables = {}; // define tables to verify
 
     tc.preTest(tables, prepDataFiles, expectedDataFiles);
 
     // execute test
 
     tc.postTest();
 }
 

When all test methods share the same prep and/or expected data

If each test method can share all of the prep and/or expected data, then use setUp() for the configureTest() and preTest() calls and tearDown() for the postTest() call. The methods will look something like the following:
 @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.

Sharing common (but not all) prep or expected data among test methods.

Put common data in one or more files and pass the needed ones in the correct data file array.

Notes

  1. For additional examples, refer to the ITs (listed in the See Also section).
  2. To change the setup or teardown operation (e.g. change the teardown to org.dbunit.operation.DatabaseOperation.DELETE_ALL), set the setUpOperation or tearDownOperation property on the databaseTester.
  3. To set DatabaseConfig features/properties, one way is to extend this class and override the setUpDatabaseConfig(DatabaseConfig config) method from DatabaseTestCase.

Since:
2.4.8
Version:
$Revision$ $Date$
Author:
Jeff Jensen jeffjensen AT users.sourceforge.net, Last changed by: $Author$
See Also:
org.dbunit.DefaultPrepAndExpectedTestCaseDiIT, org.dbunit.DefaultPrepAndExpectedTestCaseExtIT

Constructor Summary
DefaultPrepAndExpectedTestCase()
          Create new instance.
DefaultPrepAndExpectedTestCase(DataFileLoader dataFileLoader, IDatabaseTester databaseTester)
          Create new instance with specified dataFileLoader and databasetester.
DefaultPrepAndExpectedTestCase(java.lang.String name)
          Create new instance with specified test case name.
 
Method Summary
 ITable applyColumnFilters(ITable table, java.lang.String[] excludeColumns, java.lang.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, java.lang.String[] prepDataFiles, java.lang.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(java.lang.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, java.lang.String[] prepDataFiles, java.lang.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, java.lang.String[] excludeColumns, java.lang.String[] includeColumns)
          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
getConnection
 
Methods inherited from class org.dbunit.DatabaseTestCase
closeConnection, getOperationListener, getSetUpOperation, getTearDownOperation, setUp, setUpDatabaseConfig, tearDown
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, 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, fail, fail, failNotEquals, failNotSame, failSame
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

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(java.lang.String name)
Create new instance with specified test case name.

Parameters:
name - The test case name.
Method Detail

newDatabaseTester

public IDatabaseTester newDatabaseTester()
                                  throws java.lang.Exception
Creates a new IDatabaseTester. Default implementation returns a PropertiesBasedJdbcDatabaseTester. This implementation returns the databaseTester set by the test.

Overrides:
newDatabaseTester in class DBTestCase
Throws:
java.lang.Exception

getDataSet

public IDataSet getDataSet()
                    throws java.lang.Exception
Returns the test dataset. Returns the prep dataset.

Specified by:
getDataSet in class DatabaseTestCase
Throws:
java.lang.Exception

configureTest

public void configureTest(VerifyTableDefinition[] tables,
                          java.lang.String[] prepDataFiles,
                          java.lang.String[] expectedDataFiles)
                   throws java.lang.Exception
Configure the test. Call this method before performing the test steps.

Specified by:
configureTest in interface PrepAndExpectedTestCase
Parameters:
tables - 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.
Throws:
java.lang.Exception

preTest

public void preTest()
             throws java.lang.Exception
Execute pre-test steps. Call this method before performing the test steps.

Specified by:
preTest in interface PrepAndExpectedTestCase
Throws:
java.lang.Exception

preTest

public void preTest(VerifyTableDefinition[] tables,
                    java.lang.String[] prepDataFiles,
                    java.lang.String[] expectedDataFiles)
             throws java.lang.Exception
Convenience method to call configureTest() and preTest().

Specified by:
preTest in interface PrepAndExpectedTestCase
Parameters:
tables - 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.
Throws:
java.lang.Exception

postTest

public void postTest()
              throws java.lang.Exception
Execute all post-test steps. Call this method after performing the test steps.

Specified by:
postTest in interface PrepAndExpectedTestCase
Throws:
java.lang.Exception

postTest

public void postTest(boolean verifyData)
              throws java.lang.Exception
Execute post-test steps. Call this method after performing the test steps.

Specified by:
postTest in interface PrepAndExpectedTestCase
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:
java.lang.Exception

cleanupData

public void cleanupData()
                 throws java.lang.Exception
Cleanup tables specified in prep and expected datasets, using the provided databaseTester. See IDatabaseTester.onTearDown().

Specified by:
cleanupData in interface PrepAndExpectedTestCase
Throws:
java.lang.Exception

setupData

public void setupData()
               throws java.lang.Exception
Use the provided databaseTester to prep the database with the provided prep dataset. See IDatabaseTester.onSetup().

Throws:
java.lang.Exception

verifyData

public void verifyData()
                throws java.lang.Exception
For the provided VerifyTableDefinitions, verify each table's actual results are as expected. Uses the connection from the provided databaseTester.

Throws:
java.lang.Exception

verifyData

public void verifyData(ITable expectedTable,
                       ITable actualTable,
                       java.lang.String[] excludeColumns,
                       java.lang.String[] includeColumns)
                throws DatabaseUnitException
For 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. See DefaultColumnFilter.excludeColumn(String) .
includeColumns - The column names to only include in comparison. See DefaultColumnFilter.includeColumn(String) .
Throws:
DatabaseUnitException

makeCompositeDataSet

public IDataSet makeCompositeDataSet(java.lang.String[] dataFiles)
                              throws DataSetException
Make a IDataSet from the specified files.

Parameters:
dataFiles - Represents the array of dbUnit data files.
Returns:
The composite dataset.
Throws:
DataSetException - On dbUnit errors.

applyColumnFilters

public ITable applyColumnFilters(ITable table,
                                 java.lang.String[] excludeColumns,
                                 java.lang.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

getPrepDataset

public IDataSet getPrepDataset()
Get the prep dataset, created from the prepDataFiles.

Specified by:
getPrepDataset in interface PrepAndExpectedTestCase
Returns:
The prep dataset.

getExpectedDataset

public IDataSet getExpectedDataset()
Get the expected dataset, created from the expectedDataFiles.

Specified by:
getExpectedDataset in interface PrepAndExpectedTestCase
Returns:
The expected dataset.

getDatabaseTester

public IDatabaseTester getDatabaseTester()
Get the databaseTester.

Overrides:
getDatabaseTester in class DatabaseTestCase
Returns:
The databaseTester.
See Also:
databaseTester}.

setDatabaseTester

public void setDatabaseTester(IDatabaseTester databaseTester)
Set the databaseTester.

Parameters:
databaseTester - The databaseTester to set.
See Also:
databaseTester}.

getDataFileLoader

public DataFileLoader getDataFileLoader()
Get the dataFileLoader.

Returns:
The dataFileLoader.
See Also:
dataFileLoader}.

setDataFileLoader

public void setDataFileLoader(DataFileLoader dataFileLoader)
Set the dataFileLoader.

Parameters:
dataFileLoader - The dataFileLoader to set.
See Also:
dataFileLoader}.

setPrepDs

public void setPrepDs(IDataSet prepDs)
Set the prepDs.

Parameters:
prepDs - The prepDs to set.
See Also:
prepDs}.

setExpectedDs

public void setExpectedDs(IDataSet expectedDs)
Set the expectedDs.

Parameters:
expectedDs - The expectedDs to set.
See Also:
expectedDs}.

getTableDefs

public VerifyTableDefinition[] getTableDefs()
Get the tableDefs.

Returns:
The tableDefs.
See Also:
tableDefs}.

setTableDefs

public void setTableDefs(VerifyTableDefinition[] tableDefs)
Set the tableDefs.

Parameters:
tableDefs - The tableDefs to set.
See Also:
tableDefs}.


Copyright © 2002-2010. All Rights Reserved.