您的位置:軟件測(cè)試 > 開(kāi)源軟件測(cè)試 > 開(kāi)源單元測(cè)試工具 > DBunit
DBUnit 進(jìn)行單元測(cè)試
作者:網(wǎng)絡(luò)轉(zhuǎn)載 發(fā)布時(shí)間:[ 2013/6/28 13:22:03 ] 推薦標(biāo)簽:

我不喜歡繼承dbunit的類(lèi),所以我們?cè)贘unitTest 的類(lèi)里增加這個(gè)變量:

    public class Test2 extends TestCase {    
       
        private IDatabaseTester databaseTester;    
       
        protected void setUp() throws Exception { }    
       
        protected void tearDown() throws Exception { }    
          
    }   
     
    public class Test2 extends TestCase { 
     
        private IDatabaseTester databaseTester; 
     
        protected void setUp() throws Exception {} 
     
        protected void tearDown() throws Exception {} 
       
    } 

 

然后,我們可以該寫(xiě) setUp() 方法了,無(wú)非是連接數(shù)據(jù)庫(kù),把數(shù)據(jù)倒入到表里。

    protected void setUp() throws Exception {    
            
        databaseTester = new JdbcDatabaseTester("com.mysql.jdbc.Driver","jdbc:mysql://127.0.0.1:3306/test", "root", "123");    
            
        IDataSet dataSet = getDataSet();    
            
        databaseTester.setDataSet( dataSet );    
        databaseTester.onSetup();    
    }    
       
    protected IDataSet getDataSet() throws Exception    
       {    
            
           return new FlatXmlDataSet(new FileInputStream(new File("dataset.xml")));    
       }   
        protected void setUp() throws Exception { 
             
            databaseTester = new JdbcDatabaseTester("com.mysql.jdbc.Driver","jdbc:mysql://127.0.0.1:3306/test", "root", "123"); 
             
            IDataSet dataSet = getDataSet(); 
             
            databaseTester.setDataSet( dataSet ); 
            databaseTester.onSetup(); 
        } 
     
        protected IDataSet getDataSet() throws Exception 
        { 
             
            return new FlatXmlDataSet(new FileInputStream(new File("dataset.xml"))); 
        } 


然后是 tearDown 方法

    protected void tearDown() throws Exception    
       {    
       
        databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL);    
           databaseTester.onTearDown();    
       }   
     
        protected void tearDown() throws Exception 
        { 
     
            databaseTester.setTearDownOperation(DatabaseOperation.DELETE_ALL); 
            databaseTester.onTearDown(); 
        } 

上一頁(yè)12345下一頁(yè)
軟件測(cè)試工具 | 聯(lián)系我們 | 投訴建議 | 誠(chéng)聘英才 | 申請(qǐng)使用列表 | 網(wǎng)站地圖
滬ICP備07036474 2003-2017 版權(quán)所有 上海澤眾軟件科技有限公司 Shanghai ZeZhong Software Co.,Ltd