首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

基于Spring事宜的集成测试

2012-11-21 
基于Spring事务的集成测试????test??????????? bbs.spring.common.test......!-- hibernateTemplate ....

基于Spring事务的集成测试

????test

??????????? bbs.spring.common.test

......<!-- hibernateTemplate ..................................................... --> <bean id="hibernateTemplate" ref="sessionFactory" /> </bean> <!-- transactionManager .................................................... --> <bean id="transactionManager" /> </property> </bean> <!-- 事务代理工厂bean模板 ................................................. --> <bean id="baseTransactionProxy" abstract="true" ref="transactionManager" /> <property name="proxyTargetClass" value="true" /> <property name="transactionAttributes"> <props> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> <!-- 实例化Dao .............................................................. --> <bean id="topicDao" ref="sessionFactory" /> </bean> <!-- Transactional proxy for the Application primary business object ..... --> <bean id="topicServiceTarget" ref="topicDao" /> </bean> <!-- TransactionProxyFactoryBean ........................................... --> <bean id="topicService" parent="baseTransactionProxy"> <description /> <property name="proxyInterfaces"> <list> <value> org.eesite.bbs.spring.service.ITopicService </value> </list> </property> <property name="target"> <ref bean="topicServiceTarget" /> </property> </bean>

package bbs.spring.common.test; import org.springframework.orm.hibernate3.HibernateTemplate;import org.springframework.test.AbstractTransactionalDataSourceSpringContextTests; /** * 扩展Spring抽象事务测试类, 注入hibernateTemplate, 实现getConfigLocations方法 * * @author zhanjia * */public abstract class BaseTransactionalIntegrationTests extends AbstractTransactionalDataSourceSpringContextTests { private HibernateTemplate hibernateTemplate; /** * @return the hibernateTemplate */ public HibernateTemplate getHibernateTemplate() { return hibernateTemplate; } /** * @param hibernateTemplate * the hibernateTemplate to set */ public void setHibernateTemplate(HibernateTemplate hibernateTemplate) { this.hibernateTemplate = hibernateTemplate; } /* * (non-Javadoc) * * @see org.springframework.test.AbstractDependencyInjectionSpringContextTests#getConfigLocations() */ @Override protected String[] getConfigLocations() { setAutowireMode(AUTOWIRE_BY_NAME); return new String[] { "classpath:bbs/spring/common/test/applicationContext.xml", "classpath:bbs/spring/service/test/applicationContext-test.xml" }; } }

<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans>    <bean id="topicServiceImplTest"        ref="topicService" />        <property name="hibernateTemplate" ref="hibernateTemplate" />    </bean></beans>

package bbs.spring.service.test; import java.util.List; import org.apache.commons.logging.Log;import org.apache.commons.logging.LogFactory;import org.eesite.bbs.hibernate.vo.Sort;import org.eesite.bbs.hibernate.vo.Topic;import org.eesite.bbs.hibernate.vo.User;import org.eesite.bbs.spring.service.ITopicService; import bbs.spring.common.test.BaseTransactionalIntegrationTests; /** * 主题测试类 * * @author zhanjia * */public class TopicServiceImplTest extends BaseTransactionalIntegrationTests { private static final Log log = LogFactory .getLog(TopicServiceImplTest.class); private ITopicService topicService; /** * @return the topicService */ public ITopicService getTopicService() { return topicService; } /** * @param topicService * the topicService to set */ public void setTopicService(ITopicService topicService) { this.topicService = topicService; } public void testInsertTopic() { this.deleteFromTables(new String[] { "Topic", "Sort", "User" }); // this.jdbcTemplate.execute("INSERT INTO ..."); Sort sort = new Sort(); sort.setId(new Long(1)); sort.setSortName("分类1"); sort.setDescription(""); User user = new User(); user.setId(new Long(1)); user.setUserName("zhanjia"); Topic topic = new Topic(); topic.setId(new Long(1)); topic.setTopicName("标题名"); topic.setContent(""); topic.setIcon(""); topic.setSort(sort); topic.setUser(user); this.getTopicService().insertTopic(topic); List list = this.getHibernateTemplate().find("from Topic"); assertEquals(1, list.size()); Long count = this.jdbcTemplate.queryForLong("select count(*) from Topic"); assertEquals(1, count.intValue()); }}

?

热点排行