Database Testing with DbUnit

DbUnit :DbUnit is an open source Framework created
by Manuel Laflamme. This is a powerful tool for simplifying Unit Testing of the database operations.
It extends the popular JUnit test framework that puts the database into a known
state while the test executes. This strategy helps to avoid the problem that can occur
when one test corrupts the database and causes subsequent test to fail. DbUnit
provides a very simple XML based mechanism for loading the test data, in the form of
data set in XML file, before a test runs. Moreover the database can be placed back
into its pre-test state at the completion of the test.

Advantages of DbUnit :The reasons to use this testing tool can be summarized as follows :

A framework which simplifies operations for each stage in the life cycle of individual database tests.

It provides a very simple XML based mechanism for loading test data.

It providesequally a simple mechanism to
export existing test data into the XML format
for subsequent use.

It can work with very large datasets.

It can help verify your data matches an expected set of values.

It provides methods for comparing data between flat files, queries and database tables.

DbUnit test Life Cycle :DbUnit framework follows some steps in its life cycle :

Removing the old data left in the database from previous tests.

Loading some data from XML file into the database.

Running the test.

DatabaseTestCase class provides two methods setUp() and
TearDown() which internally call getSetUpOperation() and getTearDownOperation()
methods respectively. setUp() method provides the setup operation DatabaseOperation.CLEAN_INSERT
or DatabaseOperation.REFRESH. DatabaseOperation.CLEAN_INSERT operation is the
combination of two operations DELETE_ALL and INSERT. So data defined in the XML
file is loaded in the database by this operation. First two steps of the
life cycle are executed when executing the setUp() method before running the
test. These steps allow you not to waste time in writing code to restore state
in the database. DatabaseOperation.REFRESH updates the desired database with the data found in the
XML file.
The getTearDownOperation() performs a NONE operation which does nothing.

Create XML file (for example, "input.xml")
representing the database tables and
the data within it. Put a data set in this file like below.
In this file "login" is the table name in the database and "id",
"empcode" etc are the columns in the table. Put values for the fields in this file.input.xml :

DbUnit framework provides an abstract class named
"DatabaseTestCase" which is a sub class of JUnit's "TestCase"
class. So instead of creating a subclass of TestCase class we need to extend
DatabaseTestCase class. This class provides two abstract methods "getConnection()" and "getDataSet()".

IDatabaseConnectiongetConnection() throws Exception

protected
IDataSetgetDataSet()throws Exception.

Because of its being an abstract class we need to
implement these two methods:

getConnection() method returns IDatabaseConnection object that
represents database connection created using
DriverManager class. In the above code, IDatabaseConnection represents
MySQL
database where hrapptest is the name of database where username and
password
both are "deepak".getDataSet() method uses the FlatXmlDataSet class to load "input.xml"
file and return this loaded data set as an object implementing IDataSet
interface. IDataSet provides many useful methods to return data sets.

Writing Test :Now, write test to check that the data has been loaded in TestDbUnit.java
file:

Running Test :Now, in Eclipse, go to Run->Run As and click "JUnit
Test" to run tests. If testing is successful then a green strip appears
at the left of the eclipse window. If any of the test fails then it turns into a red strip indicating
failure of any test.