智能建站平台z江苏营销型网站策划
web/
2025/10/2 23:37:09/
文章来源:
智能建站平台z,江苏营销型网站策划,网站建设销售话术900句,中企动力做销售的感受我有罪#xff0c;直到现在才写集成测试#xff08;至少针对数据库相关事务#xff09;。 因此#xff0c;为了消除内感#xff0c;我阅读了如何在周末以最少的努力实现这一目标。 提供了一个小示例#xff0c;描述了如何使用Spring和Hibernate轻松实现这一目标。 通过集… 我有罪直到现在才写集成测试至少针对数据库相关事务。 因此为了消除内感我阅读了如何在周末以最少的努力实现这一目标。 提供了一个小示例描述了如何使用Spring和Hibernate轻松实现这一目标。 通过集成测试您可以测试DAO数据访问对象层而无需部署应用程序。 对我来说这是一个巨大的优势因为现在我甚至可以在不运行应用程序的情况下测试我的条件命名查询和排序。 休眠中有一个属性可让您指定初始化会话工厂时要运行的sql脚本。 这样我现在可以用DAO层所需的数据填充表。 属性如下 prop keyhibernate.hbm2ddl.import_filesimport.sql/prop 根据hibernate 文档 您可以有许多以逗号分隔的sql脚本。这里的一个陷阱是您无法使用该脚本创建表。 因为需要首先创建架构才能运行脚本。 即使您在脚本中发出了create table语句执行脚本时也会忽略该语句正如我所看到的那样。 让我首先向您展示我要测试的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);}
} 没什么大不了的只是一个简单的DAO它有两种方法一种是持久化另一种是检索。 对我来说测试检索方法需要用一些数据填充Employee表。 这是前面介绍的导入sql脚本起作用的地方。 import.sql文件如下所示 insert into Employee (empId,emp_name) values (1,Emp test); 这只是一个基本脚本我在其中将一条记录插入到employee表中。 在此再次注意employee表应该通过hibernate auto create DDL选项创建以便运行sql脚本。 更多信息可以在这里找到。 同样我实例中的import.sql脚本也位于类路径中。 这是为了在创建Session工厂时能够将其拾取而执行的。 接下来让我们看看使用Spring运行集成测试有多么容易。 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(locationsclasspath:spring-context.xml)
TransactionConfiguration(defaultRollbacktrue,transactionManagertransactionManager)
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上下文中运行测试。 为此 我们使用SpringJUnit4ClassRunner 。 还将transction属性设置为defaultRollback true。 请注意对于MySQL要使其正常工作您的表必须设置InnoDB引擎因为MyISAM引擎不支持事务。 最后我介绍了弹簧配置它可以将所有东西连接起来 ?xml version1.0 encodingUTF-8?
beans xmlnshttp://www.springframework.org/schema/beansxmlns:xsihttp://www.w3.org/2001/XMLSchema-instance xmlns:aophttp://www.springframework.org/schema/aopxmlns:txhttp://www.springframework.org/schema/tx xmlns:contexthttp://www.springframework.org/schema/contextxsi: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.xsdcontext:component-scan base-packagecom.unittest.session.example1 /context:annotation-config /tx:annotation-driven /bean idsessionFactoryclassorg.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBeanproperty namepackagesToScanlistvaluecom.unittest.session.example1.**.*/value/list/propertyproperty namehibernatePropertiespropsprop keyhibernate.dialectorg.hibernate.dialect.MySQLDialect/propprop keyhibernate.connection.driver_classcom.mysql.jdbc.Driver/propprop keyhibernate.connection.urljdbc:mysql://localhost:3306/hbmex1/propprop keyhibernate.connection.usernameroot/propprop keyhibernate.connection.passwordpassword/propprop keyhibernate.show_sqltrue/propprop keyhibernate.dialectorg.hibernate.dialect.MySQLDialect/prop!-- --prop keyhibernate.hbm2ddl.autocreate/propprop keyhibernate.hbm2ddl.import_filesimport.sql/prop/props/property/beanbean idempDAOclasscom.unittest.session.example1.dao.hibernate.EmployeeHibernateDAOImplproperty namesessionFactory refsessionFactory //beanbean idtransactionManagerclassorg.springframework.orm.hibernate3.HibernateTransactionManagerproperty namesessionFactory refsessionFactory //bean/beans 就是这样。 我个人宁愿使用重量更轻的内存数据库例如hsqldb 来运行集成测试。 这是供任何想运行该程序并尝试使用它的人的eclipse项目。 参考来自My Journey Through IT博客的JCG合作伙伴 Dinuka Arseculeratne 与Spring Hibernate进行集成测试有多酷 。 翻译自: https://www.javacodegeeks.com/2012/11/how-cool-is-integration-testing-with-spring-and-hibernate.html
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/web/85896.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!