gettingstarted

DBunit gettingstarted

gettingstarted

Getting Started

     

We have used MySQL for database purpose. Now follow the steps below:

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                                      
loginenabled  varchar(1)   latin1_swedish_ci  NO                                       

 

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>

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()".

IDatabaseConnection getConnection() throws Exception
protected IDataSet getDataSet() throws Exception.

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

TestDbUnit.java :

...........................
...........................
// Provide a connection to the database
protected IDatabaseConnection getConnection() throws Exception{
	Class driverClass = Class.forName("com.mysql.jdbc.Driver");
	Connection jdbcConnection = 
	      DriverManager.getConnection("jdbc:mysql://localhost:
				3306/hrapptest", "root", "root");
	return new DatabaseConnection(jdbcConnection);
}
// Load the data which will be inserted for the test
protected IDataSet getDataSet() throws Exception{
	loadedDataSet = 
                 new FlatXmlDataSet(this.getClass().getClassLoader().
			getResourceAsStream("input.xml"));
	return loadedDataSet;
}
............................
............................

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:

............................
............................
public void testCheckLoginDataLoaded() throws Exception{
        assertNotNull(loadedDataSet);
        int rowCount = loadedDataSet.getTable(TABLE_LOGIN).getRowCount();
        assertEquals(2, rowCount);
}
............................
............................

Combining all of the above functionalities into one TestDbUnit.java file, you will find it as follows :
TestDbUnit.java :

package test;

import java.sql.Connection;
import java.sql.DriverManager;
import org.dbunit.DatabaseTestCase;
import org.dbunit.database.DatabaseConnection;
import org.dbunit.database.IDatabaseConnection;
import org.dbunit.dataset.IDataSet;
import org.dbunit.dataset.xml.FlatXmlDataSet;
public class TestDbUnit extends DatabaseTestCase{
     public static final String TABLE_LOGIN = "login";
     private FlatXmlDataSet loadedDataSet;
     // Provide a connection to the database
     protected IDatabaseConnection getConnection() throws Exception{
       Class.forName("com.mysql.jdbc.Driver");
       Connection jdbcConnection = 
       DriverManager.getConnection("jdbc:mysql://localhost:3306/hrapptest", 
        "root", "root");
       return new DatabaseConnection(jdbcConnection);
     }
     // Load the data which will be inserted for the test
     protected IDataSet getDataSet() throws Exception{
       loadedDataSet = 
       new FlatXmlDataSet(this.getClass().getClassLoader()
        .getResourceAsStream("input.xml"));
       return loadedDataSet;
     }
     // Check that the data has been loaded.
     public void testCheckLoginDataLoaded() throws Exception{
       assertNotNull(loadedDataSet);
       int rowCount = loadedDataSet.getTable(TABLE_LOGIN).getRowCount();
       assertEquals(2, rowCount);
     }
}

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.