private SessionFactory createTestSessionFactory() throws Exception { // create a FactoryBean to help create a Hibernate SessionFactory LocalSessionFactoryBean factoryBean = new LocalSessionFactoryBean(); factoryBean.setDataSource(createTestDataSource()); Resource[] mappingLocations = new ClassPathResource[] { new ClassPathResource("Account.hbm.xml", Account.class), new ClassPathResource("Beneficiary.hbm.xml", Beneficiary.class) }; factoryBean.setMappingLocations(mappingLocations); factoryBean.setHibernateProperties(createHibernateProperties()); // initialize according to the Spring InitializingBean contract factoryBean.afterPropertiesSet(); // get the created session factory return (SessionFactory) factoryBean.getObject(); }
public void testLocalSessionFactoryBeanWithCustomSessionFactory() throws Exception { MockControl factoryControl = MockControl.createControl(SessionFactory.class); final SessionFactory sessionFactory = (SessionFactory) factoryControl.getMock(); sessionFactory.close(); factoryControl.setVoidCallable(1); factoryControl.replay(); LocalSessionFactoryBean sfb = new LocalSessionFactoryBean() { protected SessionFactory newSessionFactory(Configuration config) { return sessionFactory; } }; sfb.setMappingResources(new String[0]); sfb.setDataSource(new DriverManagerDataSource()); sfb.setExposeTransactionAwareSessionFactory(false); sfb.afterPropertiesSet(); assertTrue(sessionFactory == sfb.getObject()); sfb.destroy(); factoryControl.verify(); }