| 
            
              | 
                  
                     |  | 
                      
                        | 
                            
                              | DbTesting with DbUnit |  
                              |  |  
                              | 
                                  
                                    | 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 functionalitiesinto 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 =
 |  |  
                              |  Sept  2007 | Java Jazz Up | 42 |  
                              | 
                                
                                  |  |  
                                  |  |  
                                  |  |  
                                | 
  
| 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
 |  |  |  |  |  |  |  
              |  |  
              |  |  |