JavaEE开源博客-阿Q

        ~用心做事,诚信做人!以德为本,兼顾各方!细心观察,联系推理!
        ~遵守规范->重构->可复用代码->可扩展性 *大道至简*
        ~时间是挤出来的,要成功,你就必须在最短的时间内采取最大量的行动!坚持到底,看到的就是胜利!再低下、再简单、再不愿意做的事情,你也要把它当成自己的事业做好!以马内利,阿们!

公告

我的分类(专题)

最新日志

最新评论

留言簿

搜索

登陆

友情连接

统计

2008-6-18 13:45:00
基于Spring事务的集成测试


一、 测试类和配置文件的目录结构
 test
            bbs.spring.common.test
                BaseTransactionalIntegrationTests
                applicationContext.xml

            bbs.spring.service.test
                TopicServiceImplTest
                applicationContext-test.xml


二、 applicationContext.xml配置
配置数据源、事务和依赖注入,暴露带事务的业务接口。
......
<!-- hibernateTemplate ..................................................... -->
 <bean id="hibernateTemplate"
  class="org.springframework.orm.hibernate3.HibernateTemplate">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>
 

 <!-- transactionManager .................................................... -->
 <bean id="transactionManager"
  class="org.springframework.orm.hibernate3.HibernateTransactionManager">
  <property name="sessionFactory">
   <ref local="sessionFactory" />
  </property>
 </bean>


 <!-- 事务代理工厂bean模板 ................................................. -->
 <bean id="baseTransactionProxy" abstract="true"
  class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
  <property name="transactionManager" ref="transactionManager" />
  <property name="proxyTargetClass" value="true" />
  <property name="transactionAttributes">
   <props>
    <prop key="*">PROPAGATION_REQUIRED</prop>
   </props>
  </property>
 </bean>


 <!-- 实例化Dao .............................................................. -->
 <bean id="topicDao"
  class="org.eesite.bbs.hibernate.dao.TopicDaoImpl">
  <property name="sessionFactory" ref="sessionFactory" />
 </bean>


 <!-- Transactional proxy for the Application primary business object ..... -->
 <bean id="topicServiceTarget"
  class="org.eesite.bbs.spring.service.TopicServiceImpl">
  <property name="topicDao" 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>

三、 扩展Spring抽象事务测试类
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" };
 }

}


四、 applicationContext-test.xml配置
为测试类注入业务接口和hibernateTemplate
<?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"
  class="bbs.spring.service.test.TopicServiceImplTest">
  <property name="topicService" 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());
 }
}

 


个人博客: http://zhan.zhmy.com/

 

posted @ 2008-6-18 13:45:00 sam 阅读全文 | 回复(0) | 引用通告 | 编辑
  • 标签:spring 测试 事务 集成 hibernate 单元 
  • 发表评论:
    网志中国数据载入中...