ArticleS. DavidChelimsky.
FitNesseAndHibernate
Last modified by david on Feb 08, 2006 at 08:59:33 PM
Home | Recent Titles | News | RSS | Comments | Guest List | Contact Us | Help

FitNesse and Hibernate


Problem: You're using FitNesse to test a legacy application that uses Hibernate to support the DAO layer. You can't use the DAO layer as/is in the FitNesse execution environment because Hibernate is configured to run as a service in a j2ee container. The object graphs provided by the DAO layer are quite complex and all the mapping logic exists in Hibernate configuration files. You don't want to maintain two sets of configuration files (one for in-container and one for outside the container).

Solution: Hibernate allows you to set configuration properties in .xml and .properties files, and will merge them if both are found on the class path. Put all of the mapping properties in hibernate.config.xml, but put all of the database connection (and pooling) properties in .properties files - one to run inside a container and one for outside. You can then use your build script to put the appropriate .properties file in the distribution archive depending on whether you're deploying to your application server or your FitNesse server.

You probably still want to have a test (at least one - probably only one is necessary) that verifies the connection under the j2ee container - probably using Cactus.


!commentForm
 Tim
Here's how we (David and myself) were able to test Hibernate DAOs that use the HibernateContext? for session and transaction management:
public void testSelectGeoGroupList() throws Exception{
        String sessionFactoryName = "test";
        //Set up a stand-alone session factory
        Configuration config = new Configuration().configure();
        SessionFactory sessionFactory = config.buildSessionFactory();        
        
        //Create a mock context and bind to a mock JNDI tree
        MockContextFactory.setAsInitial();
        Context context = new InitialContext();
        context.bind(sessionFactoryName, sessionFactory);
        
        //Mock up a transaction and a transaction manager and bind the manager to jndi
        MockControl mockTransactionControl = MockControl.createControl(Transaction.class);
        Transaction mockTrans = (Transaction)mockTransactionControl.getMock();
        MockControl mockTransactionManagerControl = MockControl.createControl(TransactionManager.class);
        TransactionManager mockTransMan = (TransactionManager) mockTransactionManagerControl.getMock();
        mockTransMan.getTransaction();
        mockTransactionManagerControl.setDefaultReturnValue(mockTrans);
        mockTransactionManagerControl.replay();
        context.bind("java:/TransactionManager",mockTransMan);
        
        //instatiate the dao passing in the jndi lookup string
        HibernateGeoGroupDAO hggDAO = new HibernateGeoGroupDAO(sessionFactoryName);
        
        //execute the method
        assertNotNull(hggDAO.selectGeoGroupList("RADIO", "FA03"));
    }


In addition, you will also need to have a stand alone config file (hibernate.cfg.xml) - DaC

Front Page | Recent Changes