javascript
使用Spring和Hibernate进行集成测试有多酷
我有罪,直到現(xiàn)在才寫(xiě)集成測(cè)試(至少針對(duì)數(shù)據(jù)庫(kù)相關(guān)事務(wù))。 因此,為了消除內(nèi)感,我閱讀了如何在周末以最少的努力實(shí)現(xiàn)這一目標(biāo)。 提供了一個(gè)小示例,描述了如何使用Spring和Hibernate輕松實(shí)現(xiàn)這一目標(biāo)。 通過(guò)集成測(cè)試,您可以測(cè)試DAO(數(shù)據(jù)訪問(wèn)對(duì)象)層,而無(wú)需部署應(yīng)用程序。 對(duì)我來(lái)說(shuō),這是一個(gè)巨大的優(yōu)勢(shì),因?yàn)楝F(xiàn)在我甚至可以在不運(yùn)行應(yīng)用程序的情況下測(cè)試我的條件,命名查詢和排序。
休眠中有一個(gè)屬性,可讓您指定初始化會(huì)話工廠時(shí)要運(yùn)行的sql腳本。 這樣,我現(xiàn)在可以用DAO層所需的數(shù)據(jù)填充表。 屬性如下:
<prop key='hibernate.hbm2ddl.import_files'>import.sql</prop>根據(jù)hibernate 文檔 ,您可以有許多以逗號(hào)分隔的sql腳本。這里的一個(gè)陷阱是您無(wú)法使用該腳本創(chuàng)建表。 因?yàn)樾枰紫葎?chuàng)建架構(gòu)才能運(yùn)行腳本。 即使您在腳本中發(fā)出了create table語(yǔ)句,執(zhí)行腳本時(shí)也會(huì)忽略該語(yǔ)句,正如我所看到的那樣。
讓我首先向您展示我要測(cè)試的DAO課;
package com.unittest.session.example1.dao;import org.springframework.transaction.annotation.Propagation; import org.springframework.transaction.annotation.Transactional;import com.unittest.session.example1.domain.Employee;@Transactional(propagation = Propagation.REQUIRED) public interface EmployeeDAO {public Long createEmployee(Employee emp);public Employee getEmployeeById(Long id); }package com.unittest.session.example1.dao.hibernate;import org.springframework.orm.hibernate3.support.HibernateDaoSupport;import com.unittest.session.example1.dao.EmployeeDAO; import com.unittest.session.example1.domain.Employee;public class EmployeeHibernateDAOImpl extends HibernateDaoSupport implementsEmployeeDAO {@Overridepublic Long createEmployee(Employee emp) {getHibernateTemplate().persist(emp);return emp.getEmpId();}public Employee getEmployeeById(Long id) {return getHibernateTemplate().get(Employee.class, id);} }沒(méi)什么大不了的,只是一個(gè)簡(jiǎn)單的DAO,它有兩種方法,一種是持久化,另一種是檢索。 對(duì)我來(lái)說(shuō),測(cè)試檢索方法需要用一些數(shù)據(jù)填充Employee表。 這是前面介紹的導(dǎo)入sql腳本起作用的地方。 import.sql文件如下所示;
insert into Employee (empId,emp_name) values (1,'Emp test');這只是一個(gè)基本腳本,我在其中將一條記錄插入到employee表中。 在此再次注意,employee表應(yīng)該通過(guò)hibernate auto create DDL選項(xiàng)創(chuàng)建,以便運(yùn)行sql腳本。 更多信息可以在這里找到。 同樣,我實(shí)例中的import.sql腳本也位于類路徑中。 這是為了在創(chuàng)建Session工廠時(shí)能夠?qū)⑵涫叭《鴪?zhí)行的。
接下來(lái),讓我們看看使用Spring運(yùn)行集成測(cè)試有多么容易。
package com.unittest.session.example1.dao.hibernate;import static org.junit.Assert.*;import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration;import com.unittest.session.example1.dao.EmployeeDAO; import com.unittest.session.example1.domain.Employee;@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations='classpath:spring-context.xml') @TransactionConfiguration(defaultRollback=true,transactionManager='transactionManager') public class EmployeeHibernateDAOImplTest {@Autowiredprivate EmployeeDAO employeeDAO;@Testpublic void testGetEmployeeById() {Employee emp = employeeDAO.getEmployeeById(1L);assertNotNull(emp);}@Testpublic void testCreateEmployee(){Employee emp = new Employee();emp.setName('Emp123');Long key = employeeDAO.createEmployee(emp);assertEquals(2L, key.longValue());}}這里要注意的幾件事是,您需要指示在Spring上下文中運(yùn)行測(cè)試。 為此 ,我們使用SpringJUnit4ClassRunner 。 還將transction屬性設(shè)置為defaultRollback = true。 請(qǐng)注意,對(duì)于MySQL,要使其正常工作,您的表必須設(shè)置InnoDB引擎,因?yàn)镸yISAM引擎不支持事務(wù)。
最后,我介紹了彈簧配置,它可以將所有東西連接起來(lái);
<?xml version='1.0' encoding='UTF-8'?> <beans xmlns='http://www.springframework.org/schema/beans'xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:aop='http://www.springframework.org/schema/aop'xmlns:tx='http://www.springframework.org/schema/tx' xmlns:context='http://www.springframework.org/schema/context'xsi:schemaLocation=' http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd'><context:component-scan base-package='com.unittest.session.example1' /><context:annotation-config /><tx:annotation-driven /><bean id='sessionFactory'class='org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean'><property name='packagesToScan'><list><value>com.unittest.session.example1.**.*</value></list></property><property name='hibernateProperties'><props><prop key='hibernate.dialect'>org.hibernate.dialect.MySQLDialect</prop><prop key='hibernate.connection.driver_class'>com.mysql.jdbc.Driver</prop><prop key='hibernate.connection.url'>jdbc:mysql://localhost:3306/hbmex1</prop><prop key='hibernate.connection.username'>root</prop><prop key='hibernate.connection.password'>password</prop><prop key='hibernate.show_sql'>true</prop><prop key='hibernate.dialect'>org.hibernate.dialect.MySQLDialect</prop><!-- --><prop key='hibernate.hbm2ddl.auto'>create</prop><prop key='hibernate.hbm2ddl.import_files'>import.sql</prop></props></property></bean><bean id='empDAO'class='com.unittest.session.example1.dao.hibernate.EmployeeHibernateDAOImpl'><property name='sessionFactory' ref='sessionFactory' /></bean><bean id='transactionManager'class='org.springframework.orm.hibernate3.HibernateTransactionManager'><property name='sessionFactory' ref='sessionFactory' /></bean></beans>就是這樣。 我個(gè)人寧愿使用重量更輕的內(nèi)存數(shù)據(jù)庫(kù)(例如hsqldb )來(lái)運(yùn)行集成測(cè)試。
這是供任何想運(yùn)行該程序并嘗試使用它的人的eclipse項(xiàng)目。
參考:來(lái)自My Journey Through IT博客的JCG合作伙伴 Dinuka Arseculeratne 與Spring + Hibernate進(jìn)行集成測(cè)試有多酷 。
翻譯自: https://www.javacodegeeks.com/2012/11/how-cool-is-integration-testing-with-spring-and-hibernate.html
總結(jié)
以上是生活随笔為你收集整理的使用Spring和Hibernate进行集成测试有多酷的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 多语言持久性:带有MongoDB和Der
- 下一篇: 跨境电商,海外狂飙