Magazine
 
DbTesting with DbUnit

DbUnit test Life Cycle :

DbUnit framework follows some steps in its life cycle :

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

2 Loading some data from XML file into the database.

3 Running the test.

DatabaseTestCase class provides two methods setUp() and TearDown() which internally call etSetUpOperation() 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.

protected void setUp() throws Exception{
super.setUp();
executeOperation
(getSetUpOperation());
}
protected void tearDown() throws
Exception{
super.tearDown();
executeOperation
(getTearDownOperation());
}
protected DatabaseOperation
getSetUpOperation() throws Exception{
return
DatabaseOperation.CLEAN_INSERT;
}
protected DatabaseOperation
getTearDownOperation() throws Exception{
return DatabaseOperation.NONE;
}

 

DbUnit can work with default behavior, however, you can override the methods according to the s requirement.

Getting Started : For the database purpose we have used MySQL

1 Create a table "login" in the database"hrapptest" in MySQL like below :

login Table in database:

Field
Type
Collation
Null
Key
Default
-----
-----
----------
----
---
-------
id
bigint(20)
(NULL)
NO
PRI
empcode
varchar(15)
latin1_swedish_ci
YES
(NULL)
loginname
varchar(30)
latin1_swedish_ci
NO
password
varchar(30)
latin1_swedish_ci
NO
oginenabledl
varchar(1)
latin1_swedish_ci
NO

2 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 :

<?xml version='1.0'
encoding='UTF-8'?>
<dataset>
<!--Login Table -->
<login id="1" empcode="E005"
loginname="chandan"
password="chandan"
loginenabled="y"/>
<login id="2" empcode="E006"
loginname="deepak"
password="deepak"
loginenabled="n"/>
</dataset>

3 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

Sept 2007 | Java Jazz Up | 41
 
previous
index
next
 
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,

30
, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53 , 54, 55, 56, 57,

58
, 59, 60, 61, 62, 63 , 64, 65 , 66 , 67 , 68 , 69 , 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,   Download PDF