DBUnit Operator
Dependency :
compile ":dbunit-operator:1.6.2"
Summary
Description
Introduction
The Dbunit-Operator integrates effortless and appropriate within DataSources configuration and helps to create initial data within database through BootStrap.groovy with help of DbUnit. Different DbUnit dataset files (Flat or Structured XML) and DbUnit operations (CLEAN_INSERT, UPDATE, etc.) can be specified for different environments. Furthermore, the DbUnit-Operator provides a simple test case DbUnitTestCase (derived from GroovyTestCase) to create jUnit/DbUnit-Integration-Tests with separate test dataset files.jUnit/DbUnit Testing
To create DbUnit test cases create a groovy class named '<name-of-tests>Tests.groovy' in the test/integration directory and derive it from the groovy class DbUnitTestCase or execute the following plugin script:grails create-dbunit-test
grails create-dbunit-test <name-of-tests>
public getDatasets() { ["data/test/data0.xml", "data/test/data1.xml"] }
- Before executing the test: CLEAN_INSERT
- Before executing the test: DELETE_ALL
// executed before test protected getDbUnitOperationType() { "CLEAN_INSERT" }
// executed after test protected getDbUnitCleanupOperationType() { "DELETE_ALL" }
- Operation specified by the method getDbUnitOperationType() will be executed for the first dataset.
- Operation INSERT will be executed for every other following dataset.
- Test is executed.
- Operation specified by the method getDbUnitCleanupOperationType() will be executed for every dataset.
grails test-app
Creating initial data
The DbUnit-Operator provides a very easy way to setup initial data. To do so, some additional attributes should be specified within DataSource.groovy:dataSource {
pooled = true
driverClassName = "org.hsqldb.jdbcDriver"
username = "sa"
password = ""
dbunitXmlType = "flat" // dbunit-operator data file type: 'flat' or 'structured'
orderTables = false // resolve table dependencies and order tables? (if true: dbunit-operator is slower)
}hibernate {
....
}// environment specific settings
environments {
development {
dataSource {
dbCreate = "create-drop" // one of 'create', 'create-drop','update'
url = "jdbc:hsqldb:mem:devDb"
initialData = "data/dev/data-a.xml, data/dev/data-a.xml" // 1-n dbunit-operator Flat-XML or XML data files
initialOperation = "CLEAN_INSERT" // dbunit-operator operation
}
}
test {
....
}
}- orderTables: Global attribute: order tables using dependency information provided by database meta data. See DbUnit database sequence filter class structures.
- dbunitXmlType: Global attribute: defines the XML structure of your initial dataset: 'flat' or 'structured'. See DbUnit data structures for definition of these XML structures.
- initialData: Can be specified for every environment. Initial dataset inserted into database.
- initialOperation: Can be specified for every environment. Initial DbUnit operation executed for the initial dataset.
class BootStrap { def init = { servletContext -> // is also executed when running tests -> provide empty or dummy initial data file within the project's root path
DbUnitOperator.create() // Create other data without DbUnit-Operator here, if you wish…
} def destroy = {
}
}- Calling DbUnitOperator.operate(operationType): The DbUnit-Operation that should be used can be specified explicitly.
- Calling DbUnitOperator.operate(filePath,operationType): The file (dataset) and DbUnit-Operation that should be used can be specified explicitly.
Which paths for dbunit data files?
- When running the grails application: <project-root>/web-app/. Used during the startup of the grails application, typically when initial data should be stored through the BootStrap: DbUnitOperator.create().
- When running the dbunit test cases: <project-root>/web-app/. Used when running 'grails test-app'.
- When defining absolute path: absolute path is used.
Scripts included with DbUnit-Operator
- create-dbunit-test: creates a DbUnit test case in the test/integration directory.
Example Files
When downloading the whole plugin structure from SVN-Repository, additional example files will be available.- <pluginDir>/grails-app/conf/BootStrap.groovy: Contains example call.
- <pluginDir>/grails-app/conf/DataSource.groovy: Contains example configuration.
- <pluginDir>/grails-app/domain/TestDoCustomer.groovy: Test domain class.
- <pluginDir>/grails-app/domain/TestDoProject.groovy: Test domain class.
- <pluginDir>/test/integration/MyTests.groovy: Example test using test domain classes and dataset data/test/data0.xml and data/test/data1.xml.
- <pluginDir>/web-app/data/test/data0.xml: Example test dataset used by MyTests.groovy (jUnit/DbUnit-integration test).
- <pluginDir>/web-app/data/test/data1.xml: Example test dataset used by MyTests.groovy (jUnit/DbUnit-integration test).
- <pluginDir>/web-app/data/init/data-a.xml: Example initial dataset used by BootStrap.groovy.
- <pluginDir>/web-app/data/init/data-b.xml: Example initial dataset used by BootStrap.groovy.
Example Project
Access to an example project using the DbUnit-Operator 1.5:Changes
Release 1.6.2
- Added support for TRUNCATE_TABLE Operation
Release 1.5
- Functionality of release 1.4 but with possibility to resolve table dependencies and order tables with orderTables configuration attribute. See DbUnit database sequence filter class structures.
Release 1.4
- Re-integration of Gert Wolgemuth's changes after package refactoring: replacement of implementation details to make sure data are imported in the right order (using org.dbunit.database.DatabaseSequenceFilter)
- Always use the servlet context real path as base for relative defined data files; root path --> web-app/
Release 1.3
Changes made by request of Lucas Frare Teixeira:- Test domain classes from the plugin package excluded, so the corresponding test tables aren't created
- DbUnit-Operator classes have be put into package ch.gstream.grails.plugins.dbunitoperator
- More than one data files can be defined within initialData section (DataSource.groovy)
Release 1.2
- InsertIdentityOperation has been imported, so DB operations with the MS SQL server are possible (MSSQL_INSERT, etc.)
- The handling of paths for dbunit data files has been made easier. Defining absolute paths within DataSource.groovy (Property 'initialData') is now possible. See also chapter Which paths and contexts are used for dbunit data files? above.