Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Spring Framework | Web Services | BioInformatics | Java Server Faces | Jboss 3.0 tutorial | Hibernate 3.0 | XML

Tutorial Categories: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML


 

Java Tutorials


 

 

Struts Tutorials

Struts Resources

Visit Forum! Post Questions!
Jobs At RoseIndia.net!

Getting Started

                         

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

  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                                      
    loginenabled  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 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.

     

  4. 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.

                         

Ask programming questions?

 

 

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

0 comments so far (post your own) View All Comments Latest 10 Comments:
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification

Tell A Friend
Your Friend Name

 

 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright 2007. All rights reserved.

[an error occurred while processing this directive]