一、 测试类和配置文件的目录结构
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());
}
}