`
shyboy0358
  • 浏览: 33579 次
文章分类
社区版块
存档分类
最新评论

一个DAO测试基类

 
阅读更多
  1. package com.taobao.dbunit.dao;   
  2.   
  3. import java.sql.SQLException;   
  4.   
  5. import javax.sql.DataSource;   
  6.   
  7. import org.dbunit.Assertion;   
  8. import org.dbunit.database.DatabaseConnection;   
  9. import org.dbunit.database.IDatabaseConnection;   
  10. import org.dbunit.dataset.DataSetException;   
  11. import org.dbunit.dataset.DefaultDataSet;   
  12. import org.dbunit.dataset.DefaultTable;   
  13. import org.dbunit.dataset.IDataSet;   
  14. import org.dbunit.dataset.xml.FlatXmlDataSet;   
  15. import org.dbunit.operation.DatabaseOperation;   
  16. import org.junit.Assert;   
  17. import org.junit.Before;   
  18. import org.springframework.beans.factory.annotation.Autowired;   
  19. import org.springframework.core.io.ClassPathResource;   
  20. import org.springframework.jdbc.datasource.DataSourceUtils;   
  21. import org.springframework.test.context.ContextConfiguration;   
  22. import org.springframework.test.context.junit4.AbstractTransactionalJUnit4SpringContextTests;   
  23. import org.springframework.test.context.transaction.TransactionConfiguration;   
  24.   
  25. @ContextConfiguration(locations = { "classpath:testApplicationContext.xml" })   
  26. @TransactionConfiguration(defaultRollback = true)   
  27. public class BaseDaoTest extends AbstractTransactionalJUnit4SpringContextTests {   
  28.   
  29.     @Autowired  
  30.     private DataSource dataSource;   
  31.   
  32.     private IDatabaseConnection conn;   
  33.   
  34.     @Before  
  35.     public void initDbunit() throws Exception {   
  36.         conn = new DatabaseConnection(DataSourceUtils.getConnection(dataSource));   
  37.     }   
  38.   
  39.     /**  
  40.      * 清空file中包含的表中的数据,并插入file中指定的数据  
  41.      *   
  42.      * @param file  
  43.      * @throws Exception  
  44.      */  
  45.     protected void setUpDataSet(String file) throws Exception {   
  46.         IDataSet dataset = new FlatXmlDataSet(new ClassPathResource(file)   
  47.                 .getFile());   
  48.         DatabaseOperation.CLEAN_INSERT.execute(conn, dataset);   
  49.     }   
  50.   
  51.     /**  
  52.      * 验证file中包含的表中的数据和数据库中的相应表的数据是否一致  
  53.      *   
  54.      * @param file  
  55.      * @throws Exception  
  56.      */  
  57.     protected void verifyDataSet(String file) throws Exception {   
  58.         IDataSet expected = new FlatXmlDataSet(new ClassPathResource(file)   
  59.                 .getFile());   
  60.         IDataSet dataset = conn.createDataSet();   
  61.   
  62.         for (String tableName : expected.getTableNames()) {   
  63.             Assertion.assertEquals(expected.getTable(tableName), dataset   
  64.                     .getTable(tableName));   
  65.         }   
  66.   
  67.     }   
  68.   
  69.     /**  
  70.      * 清空指定的表中的数据  
  71.      *   
  72.      * @param tableName  
  73.      * @throws Exception  
  74.      */  
  75.     protected void clearTable(String tableName) throws Exception {   
  76.         DefaultDataSet dataset = new DefaultDataSet();   
  77.         dataset.addTable(new DefaultTable(tableName));   
  78.         DatabaseOperation.DELETE_ALL.execute(conn, dataset);   
  79.     }   
  80.   
  81.     /**  
  82.      * 验证指定的表为空  
  83.      *   
  84.      * @param tableName  
  85.      * @throws DataSetException  
  86.      * @throws SQLException  
  87.      */  
  88.     protected void verifyEmpty(String tableName) throws DataSetException,   
  89.             SQLException {   
  90.         Assert.assertEquals(0, conn.createDataSet().getTable(tableName)   
  91.                 .getRowCount());   
  92.     }   
  93. }  
  1. @Test  
  2. public void updateUser() throws Exception {   
  •     setUpDataSet("com/taobao/dbunit/dao/user001.xml");   
  •        
  •     User user = new User();   
  •     user.setNick("user001");   
  •     user.setPassword("password002");   
  •     userDao.update(user);   
  •   
  •     verifyDataSet("com/taobao/dbunit/dao/user001_updated.xml");   
  • }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics