1. Add the DbUnit jar to Ant's classpath.
2. Add a <taskdef> element to your build script as follows:
<taskdef name="dbunit" classname="org.dbunit.ant.DbUnitTask"/>
3. Use the task in the rest of the buildfile.
Executes either a single transaction, or a group of transactions, under the DbUnit database testing framework.
Attribute | Description | Required |
---|---|---|
driver | Class name of the jdbc driver | Yes |
url | Database connection url | Yes |
userid | Database username | Yes |
password | Database password | Yes |
schema | Database schema | No |
classpath | Classpath used to load driver | No (use system classpath) |
useQualifiedTableNames | Set the qualified table names feature. Defaults to false | No |
supportBatchStatement | Set the batched statement feature. Defaults to false | No |
datatypeWarning | Set the data type warning feature. Defaults to true | No |
escapePattern | Set the escape pattern property. | No |
datatypeFactory | Set the datatype factory property. | No |
classpath | DbUnit's classpath attribute is a PATH like structure and can also be set via a nested classpath element. It is used to load the JDBC classes. |
|||||||||||||||||||||||||||||||
operation | Use nested <operation> elements to specify which DbUnit operation
to perform on the particular file.
|
|||||||||||||||||||||||||||||||
export | Use nested <export> operation elements to export the database to the supplied filename. The default operation is to <export> the entire database to the destination filename with the supplied dataset type. You can specify individual tables or queries to <export> by nesting them under the <export> step.
Parameters specified as nested elements
|
|||||||||||||||||||||||||||||||
compare | Use nested <compare> elements to validate the content of the database against the specified dataset file.
Parameters specified as nested elements
|
<dbunit driver="com.vendor.jdbc.Driver" url="jdbc:vendor:mydatabase" userid="user" password="password"> <classpath> <pathelement location="/some/jdbc.jar"/> </classpath> <operation type="UPDATE" src="updateFile.xml"/> </dbunit>
<dbunit driver="com.vendor.jdbc.Driver" url="jdbc:vendor:mydatabase" userid="user" password="password"> <operation type="INSERT" src="insertFile.xml"/> <operation type="UPDATE" src="updateFile.xml"/> </dbunit>
<dbunit driver="com.vendor.jdbc.Driver" url="jdbc:vendor:mydatabase" userid="user" password="password"> <export dest="export.xml"/> </dbunit>
<dbunit driver="com.vendor.jdbc.Driver" url="jdbc:vendor:mydatabase" userid="user" password="password"> <export dest="export.dtd" format="dtd"/> </dbunit>
Export two tables: FOO, resulting from specified query and BAR entire content
<dbunit driver="com.vendor.jdbc.Driver" url="jdbc:vendor:mydatabase" userid="user" password="password"> <export dest="partial.xml"> <query name="FOO" sql="SELECT COL1, COL2 FROM FOO WHERE COL1=4"/> <table name="BAR"/> </export> </dbunit>
<dbunit driver="com.vendor.jdbc.Driver" url="jdbc:vendor:mydatabase" userid="user" password="password"> <compare src="expectedData.xml"/> </dbunit>
<dbunit driver="com.vendor.jdbc.Driver" url="jdbc:vendor:mydatabase" userid="user" password="password"> <compare src="expectedData.xml"> <query name="FOO" sql="SELECT COL1, COL2 FROM FOO WHERE COL1=4"/> <table name="BAR"/> </compare> </dbunit>