mercredi 3 juin 2015

How to test DAO wich used unable class?

I have to write some dao test for project where I want:

  1. create DDL schema from database (MySQL);
  2. create tables in another test database in memory (H2);
  3. instert some data to databese;
  4. select just inserted item;
  5. check dome data from this item.

This is my test:

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();
    }
}

BridgeDAOImplused class HibernateUtilfrom src/main/..., but I need to use my class HibernateTestUtil from src/test/.... It's modified HibernateUtil fitted for my test (there I set parameters for Configuration class).

BridgeDAOImpl (See 5 line in try block):

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;
    }
    //...
}

How I can test my dao without modifying it?
Any ideas?

Aucun commentaire:

Enregistrer un commentaire