風人園

          弱水三千,只取一瓢,便能解渴;佛法無邊,奉行一法,便能得益。
          隨筆 - 99, 文章 - 181, 評論 - 56, 引用 - 0
          數據加載中……

          dbunit使用

           

          使用dbunit,可以幫助我們在測試中維護數據,也可以輔助我們的測試。

          首先當然是下載dbunit, http://dbunit.sourceforge.net

          我測試用的是 MYSQL 5.0 。

          建立數據庫:
          create table test1(
          id int not null auto_increment,
          user_name varchar(50),
          primary key(id)) engine=innodb;

          保存數據的xml文件:

          xml 代碼
          1. <dataset>  
          2.     <test1 user_name="tom"/>                
          3.     <test1 user_name="John"/>  
          4.     <test1 user_name="Rose"/>     
          5. </dataset>  

           

          首先建立一個 JunitTest 的類:

          java 代碼
          1. public class Test2 extends TestCase {   
          2.   
          3.     protected void setUp() throws Exception {   
          4.   
          5.          }   
          6.   
          7.     protected void tearDown() throws Exception {   
          8.   
          9.          }   
          10.      
          11. }   

           

          我不喜歡繼承dbunit的類,所以我們在JunitTest 的類里增加這個變量:

          java 代碼
          1. public class Test2 extends TestCase {   
          2.   
          3.     private IDatabaseTester databaseTester;   
          4.   
          5.     protected void setUp() throws Exception {   
          6.   
          7.          }   
          8.   
          9.     protected void tearDown() throws Exception {   
          10.   
          11.          }   
          12.      
          13. }   

           

          然后,我們可以該寫 setUp() 方法了,無非就是連接數據庫,把數據倒入到表里。

          java 代碼
          1. protected void setUp() throws Exception {   
          2.        
          3.     databaseTester = new JdbcDatabaseTester("com.mysql.jdbc.Driver",   
          4.             "jdbc:mysql://127.0.0.1:3306/test""root""123");   
          5.        
          6.     IDataSet dataSet = getDataSet();   
          7.        
          8.     databaseTester.setDataSet( dataSet );   
          9.     databaseTester.onSetup();   
          10.   
          11. }   
          12.   
          13. protected IDataSet getDataSet() throws Exception   
          14.    {   
          15.        
          16.        return new FlatXmlDataSet(new FileInputStream(new File("dataset.xml")));   
          17.    }   

           

          然后是 tearDown 方法

          java 代碼
          1. protected void tearDown() throws Exception   
          2.    {   
          3.   
          4.     databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);   
          5.        databaseTester.onTearDown();   
          6.    }   

           

          好了,準備工作完成了,下面開始寫測試方法。

          java 代碼
          1. public void test1() throws Exception{   
          2.        
          3.     ITable test1Table = databaseTester.getDataSet().getTable("test1");   
          4.     assertEquals(test1Table.getRowCount(), 3);   
          5.        
          6. }   

          這個方法就是測試 test1 表有多少條記錄

           

          java 代碼
          1. public void test2() throws Exception{   
          2.        
          3.     ITable test1Table = databaseTester.getConnection().createQueryTable("any_name",   
          4.             "select user_name from test1 where user_name='tom'");   
          5.        
          6.     assertTrue(test1Table.getRowCount()==1);   
          7.        
          8. }   

          這個方法測試是否有 user_name='tom' 這個記錄.

           

          那么自動增加字段怎么辦呢?
          只要在 tearDown 里增加

          java 代碼
          1. databaseTester.setTearDownOperation(DatabaseOperation.TRUNCATE_TABLE);   

          就可以了!

          posted on 2007-09-16 20:41 風人園 閱讀(1071) 評論(0)  編輯  收藏 所屬分類: Test


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 横山县| 扎赉特旗| 水城县| 辽宁省| 塘沽区| 洛浦县| 大余县| 承德县| 逊克县| 伽师县| 开原市| 宝清县| 石城县| 云和县| 宣城市| 永济市| 大名县| 会泽县| 贡觉县| 陆川县| 义乌市| 无极县| 乐亭县| 汉寿县| 广西| 台山市| 金坛市| 祁连县| 咸宁市| 富源县| 沁源县| 天长市| 都江堰市| 武邑县| 壶关县| 依安县| 图们市| 郑州市| 高州市| 丹棱县| 丹巴县|