如何测试使用无法上课的DAO?

时间:2015-06-03 14:03:26

标签: java hibernate testing dao

我必须为我想要的项目编写一些dao测试:

  1. 从数据库创建DDL架构(MySQL);
  2. 在内存中的另一个测试数据库中创建表(H2);
  3. 将一些数据发送给数据库;
  4. 选择刚插入的项目;
  5. 检查此项目的圆顶数据。
  6. 这是我的测试:

    public class BridgeDBTest {
    
        private static String JDBC_DRIVER;
        private static String JDBC_URL;
        private static String USER;
        private static String PSWD;
        private static final Logger logger = LoggerFactory.getLogger(BridgeDBTest.class);
    
        @BeforeGroups(groups = "bridgeDB")
        public void init(){
            try {
                JDBC_DRIVER = org.h2.Driver.class.getName();
                JDBC_URL = "jdbc:h2:mem:test;DB_CLOSE_DELAY=-1";
                USER = "root";
                PSWD = "";
    
                new HibernateTestUtil().setDialect("org.hibernate.dialect.HSQLDialect")
                        .translateCreateDllToOutputStream(new FileOutputStream(new File("src/test/resources/createSchema.sql")));
    
                RunScript.execute(JDBC_URL, USER, PSWD, "src/test/resources/createSchema.sql", Charset.forName("UTF8"), false);
    
                insertDataset(readDataSet());
    
            }
            catch (Exception expt) {
                expt.printStackTrace();
                logger.error("!!!" + expt);
                throw new RuntimeException(expt.getMessage());
            }
        }
    
    
        @Test(groups = "bridgeDB")
        public void getItem(){
            BridgeDAOImpl dao = new BridgeDAOImpl();
            dao.setSessionFactory(new HibernateTestUtil().getSessionFactory());
    
            try {
                Bridge bridge = dao.get(1L);
                assert(bridge.getName().equals("TEST-CN-DEVBOX01"));
    
            } catch (ServiceException e) {
                e.printStackTrace();
            }
        }
    
        @AfterGroups(groups = "bridgeDB")
        public void dropTables(){
            try {
                new HibernateTestUtil().setDialect("org.hibernate.dialect.HSQLDialect")
                        .translateDropDllToOutputStream(new FileOutputStream(new File("src/test/resources/dropSchema.sql")));
            }
            catch (Exception expt) {
                expt.printStackTrace();
                logger.error("!!!" + expt);
                throw new RuntimeException(expt.getMessage());
            }
        }
    
        private IDataSet readDataSet() throws Exception{
            return new FlatXmlDataSetBuilder().build(new File("src/test/resources/datasetForTest.xml"));
        }
    
        private void insertDataset(IDataSet dataSet) throws Exception{
            IDatabaseTester databaseTester = new JdbcDatabaseTester(JDBC_DRIVER, JDBC_URL, USER, PSWD);
            databaseTester.setSetUpOperation(DatabaseOperation.CLEAN_INSERT);
            databaseTester.setDataSet(dataSet);
            databaseTester.onSetup();
        }
    }
    

    BridgeDAOImpl使用了来自HibernateUtil的课程src/main/...,但我需要使用HibernateTestUtil中的课程src/test/...。修改后的HibernateUtil适合我的测试(我为Configuration类设置了参数)。

    BridgeDAOImpl(参见try区块中的5行):

    public class BridgeDAOImpl extends GenericDAOImpl<Bridge, Long> implements BridgeDAO {
    //...
    public SearchResult<Bridge> list(int from, int limit, String filter, String order, Long authId) throws ServiceException {
    
            SearchResult<Bridge> results = null;
            Search search = new Search(Bridge.class);
    
            Session session = getSessionFactory().getCurrentSession();
            Transaction transaction = null;
    
            try {
                transaction = session.beginTransaction();
    
                search.setFirstResult(from);
                search.setMaxResults(limit);
                HibernateUtil.buildSearch(filter, order, search, aliases);
                results = searchAndCount(search);
    
                transaction.commit();
            }
            catch (Exception expt) {
                logger.error("!!!", expt);
                if (transaction != null) {
                    transaction.rollback();
                }
                throw new ServiceException(expt.getMessage());
            }
            finally {
    //            session.close();
            }
            return results;
        }
        //...
    }
    

    如何在不修改的情况下测试我的dao? 有什么想法吗?

0 个答案:

没有答案