public class DbUnitModule extends Object implements Module
DataSet annotation. The name of the data set
files can be specified explicitly as an argument of the annotation. If no file name is specified, it looks for a file in the same
directory as the test class named: 'classname without packagename'.xml.
By annotating a method with the ExpectedDataSet annotation or by calling the assertDbContentAsExpected(java.lang.reflect.Method, java.lang.Object) method, the
contents of the database can be compared with the contents of a dataset. The expected dataset can be passed as an argument of the
annotation. If no file name is specified it looks for a file in the same directory as the test class that has following name: 'classname
without packagename'.'test method name'-result.xml.
This module depends on the DatabaseModule for database connection management.| Modifier and Type | Class and Description |
|---|---|
protected class |
DbUnitModule.DbUnitListener
Test listener that is called while the test framework is running tests
|
| Modifier and Type | Field and Description |
|---|---|
protected Properties |
configuration
The unitils configuration
|
protected String |
databaseName |
protected Map<String,DbUnitDatabaseConnection> |
dbUnitDatabaseConnections
Objects that D513
bUnit uses to connect to the database and to cache some database metadata.
|
protected Map<Class<? extends Annotation>,Map<String,String>> |
defaultAnnotationPropertyValues
Map holding the default configuration of the dbunit module annotations
|
static String |
PROPERTY_METAHANDLER |
protected String |
scriptExtension |
| Constructor and Description |
|---|
DbUnitModule() |
| Modifier and Type | Method and Description |
|---|---|
void |
afterInit()
No after initialization needed for this module
|
void |
assertDbContentAsExpected(Method testMethod,
Object testObject)
Compares the contents of the expected DbUnitDataSet with the contents of the database.
|
void |
assertExpectedDataSets(ExpectedDataSet expectedDataSetAnnotation,
Object testObject,
Method testMethod) |
void |
assertExpectedDataSets(MultiSchemaDataSet multiSchemaExpectedDataSet,
Object testObject) |
void |
assertMultipleExpectedDataSets(ExpectedDataSets expectedDataSets,
Object testObject,
Method testMethod) |
protected void |
closeJdbcConnection()
Closes (i.e. return to the pool) the JDBC Connection that is currently in use by the DbUnitDatabaseConnection
|
protected DataSetFileNamesHandler |
createDataSetFileNamesHandler() |
protected DbUnitDatabaseConnection |
createDbUnitConnection(String schemaName)
Creates a new instance of dbUnit's
IDatabaseConnection |
protected org.dbunit.dataset.IDataSet |
getActualDataSet(String schemaName)
Gets the actual data set for the given schema.
|
protected DatabaseModule |
getDatabaseModule() |
protected MultiSchemaDataSet |
getDataSet(Class<?> testClass,
String[] dataSetFileNames,
DataSetFactory dataSetFactory)
Creates the dataset for the given file.
|
MultiSchemaDataSet |
getDataSet(DataSet dataSet,
Method testMethod,
Class<?> testClass)
Using the values of the method-level or class-level
DataSet annotations, returns the data set for the given test method. |
protected DataSetFactory |
getDataSetFactory(Class<? extends Annotation> annotationClass,
Method testMethod,
Class<?> testClass)
Get the configured DataSetFactory for the given method
|
protected DataSetFactory |
getDataSetFactory(Class<? extends DataSetFactory> dataSetFactoryClass)
Creates and initializes a data set factory of the given type.
|
protected DataSetLoadStrategy |
getDataSetLoadStrategy(Method testMethod,
Class<?> testClass,
DataSet dataSet)
Creates the DbUnit dataset operation for loading a data set for the given method.
|
protected DataSetResolver |
getDataSetResolver() |
protected Map<String,List<DataSet>> |
getDataSetsSorted(DataSets dataSets) |
DbUnitDatabaseConnection |
getDbUnitDatabaseConnection(String schemaName)
Gets the DbUnit connection or creates one if it does not exist yet.
|
protected org.dbunit.database.IMetadataHandler |
getDefaultDatabaseMetaHandler() |
protected DataSetFactory |
getDefaultDataSetFactory() |
protected DataSetLoadStrategy |
getDefaultDataSetLoadStrategy() |
protected DbSupport |
getDefaultDbSupport() |
MultiSchemaDataSet |
getExpectedDataSet(ExpectedDataSet expectedDataSetAnnotation,
Method testMethod,
Object testObject)
Returns the
MultiSchemaDataSet that represents the state of a number of database tables after the given Method
has been executed. |
protected FileHandler |
getFileHandler() |
protected org.unitils.dbmaintainer.locator.resourcepickingstrategie.ResourcePickingStrategie |
getResourcePickingStrategie()
use unitil property instead of hardcoding
|
TestListener |
getTestListener() |
void |
init(Properties configuration)
Initializes the DbUnitModule using the given Configuration
|
void |
insertDataSet(Class<?> testClass,
String... dataSetFileNames)
Inserts the dataset consisting of the given list of files into the database
|
void |
insertDataSet(DataSet dataset,
Object testObject,
Method testMethod) |
void |
insertDataSet(File dataSetFile)
Inserts the test data coming from the given DbUnit dataset file using the default
DataSetLoadStrategy and
DataSetFactory class. |
void |
insertDataSet(File dataSetFile,
Class<? extends DataSetFactory> dataSetFactoryClass,
Class<? extends DataSetLoadStrategy> dataSetLoadStrategyClass)
Inserts the test data coming from the given DbUnit dataset file.
|
void |
insertDataSet(Method testMethod,
Object testObject)
This method will first try to load a method level defined dataset.
|
protected void |
insertDataSet(MultiSchemaDataSet multiSchemaDataSet,
DataSetLoadStrategy dataSetLoadStrategy)
Loads the given multi schema dataset into the database, using the given loadstrategy
|
void |
insertDataSets(DataSets dataSets,
Object testObject,
Method testMethod) |
void |
insertDefaultDataSet(Class<?> testClass)
Inserts the default dataset for the given test class into the database
|
public static final String PROPERTY_METAHANDLER
protected Map<Class<? extends Annotation>,Map<String,String>> defaultAnnotationPropertyValues
protected Map<String,DbUnitDatabaseConnection> dbUnitDatabaseConnections
protected Properties configuration
protected String databaseName
protected String scriptExtension
public void init(Properties configuration)
public void afterInit()
public DbUnitDatabaseConnection getDbUnitDatabaseConnection(String schemaName)
schemaName - The schema name, not nullpublic void insertDataSet(Method testMethod, Object testObject)
DataSet annotation. If specified using this annotation but not found, a UnitilsException is
thrown.testMethod - The method, not nulltestObject - The test object, not nullpublic void insertDefaultDataSet(Class<?> testClass)
testClass - The test class for which the default dataset must be loadedpublic void insertDataSet(Class<?> testClass, String... dataSetFileNames)
testClass - The test class for which the dataset must be loadeddataSetFileNames - The names of the files that define the test datapublic void insertDataSet(File dataSetFile)
DataSetLoadStrategy and
DataSetFactory class.dataSetFile - The test data set, not nullpublic void insertDataSet(File dataSetFile, Class<? extends DataSetFactory> dataSetFactoryClass, Class<? extends DataSetLoadStrategy> dataSetLoadStrategyClass)
dataSetFile - The test data set, not nulldataSetFactoryClass - The class of the factory that must be used to read this datasetdataSetLoadStrategyClass - The class of the load strategy that must be used to load this datasetprotected void insertDataSet(MultiSchemaDataSet multiSchemaDataSet, DataSetLoadStrategy dataSetLoadStrategy)
multiSchemaDataSet - The multi schema dataset that is inserted in the databasedataSetLoadStrategy - The load strategy that is usedpublic void assertDbContentAsExpected(Method testMethod, Object testObject)
testMethod - The test method, not nulltestObject - The test object, not nullpublic void assertMultipleExpectedDataSets(ExpectedDataSets expectedDataSets, Object testObject, Method testMethod)
public void assertExpectedDataSets(ExpectedDataSet expectedDataSetAnnotation, Object testObject, Method testMethod)
public void assertExpectedDataSets(MultiSchemaDataSet multiSchemaExpectedDataSet, Object testObject)
protected org.dbunit.dataset.IDataSet getActualDataSet(String schemaName)
schemaName - The schema to get the data set for, not nullpublic MultiSchemaDataSet getDataSet(DataSet dataSet, Method testMethod, Class<?> testClass)
DataSet annotations, returns the data set for the given test method. If
no method-level or class-level DataSet annotation is found, null is returned. If a method-level DataSet annotation is
found this will be used, else the class-level will be used.
The value of the found annotation determines which files need to be used for the dataset. If one or more filenames are explicitly
specified, these names will be used. Filenames that start with '/' are treated absolute. Filenames that do not start with '/', are
relative to the current class. If an empty filename ("") is specified, this method will look for a file named 'classname'.xml.
#getDefaultDataSetFileName).
If a file is not found or could not be loaded (but was requested, because there is an annotation), an exception is thrown.testMethod - The test method, not nulltestObject - The test object, not nullDataSet annotation is found.public MultiSchemaDataSet getExpectedDataSet(ExpectedDataSet expectedDataSetAnnotation, Method testMethod, Object testObject)
MultiSchemaDataSet that represents the state of a number of database tables after the given Method
has been executed.testMethod - The test method, not nulltestObject - The test object, not nullprotected MultiSchemaDataSet getDataSet(Class<?> testClass, String[] dataSetFileNames, DataSetFactory dataSetFactory)
testClass - The test class, not nulldataSetFileNames - The names of the files, (start with '/' for absolute names), not null, not emptydataSetFactory - DataSetFactory responsible for creating the dataset fileprotected DataSetLoadStrategy getDataSetLoadStrategy(Method testMethod, Class<?> testClass, DataSet dataSet)
testMethod - The method, not nulltestClass - The test class, not nullprotected DbUnitDatabaseConnection createDbUnitConnection(String schemaName)
IDatabaseConnectionschemaName - The schema name, not nullIDatabaseConnectionprotected org.dbunit.database.IMetadataHandler getDefaultDatabaseMetaHandler()
protected void closeJdbcConnection()
protected DataSetFactory getDefaultDataSetFactory()
DataSetFactory class as configured in unitilsprotected DataSetFactory getDataSetFactory(Class<? extends Annotation> annotationClass, Method testMethod, Class<?> testClass)
annotationClass - The class of the annotation, i.e. DataSet.class or ExpectedDataSet.classtestMethod - The method for which we need the configured DataSetFactorytestClass - The class that is looked for class-level annotationsprotected DataSetFactory getDataSetFactory(Class<? extends DataSetFactory> dataSetFactoryClass)
dataSetFactoryClass - The type, not nullDataSetFactory with the given classprotected DataSetLoadStrategy getDefaultDataSetLoadStrategy()
DataSetLoadStrategy class as configured in unitilsprotected DataSetResolver getDataSetResolver()
protected DbSupport getDefaultDbSupport()
protected DatabaseModule getDatabaseModule()
protected org.unitils.dbmaintainer.locator.resourcepickingstrategie.ResourcePickingStrategie getResourcePickingStrategie()
ResourcePickingStrategieprotected DataSetFileNamesHandler createDataSetFileNamesHandler()
public TestListener getTestListener()
getTestListener in interface Moduleprotected FileHandler getFileHandler()
Copyright © 2017. All rights reserved.