Spring+hibernate dao和service层的单元测试

查了半天的资料,终于搞清楚了如何在hibernate+spring的框架下对DAO层和service层进行测试了。我把自己的经验总结一下,希望对大家有所帮助。
最重要的是applicationContext.xml的配置,
dao的配置如下
 

<bean id="CfmStorageRecordDAO"
  class="com.finegold.digimus.dao.imp.CfmStorageRecordDAOImp">
  <property name="sessionFactory">
   <ref bean="sessionFactory" />
  </property>
 </bean>

 
如果是这样的话,对于dao的测试,我想应该没有什么问题;
首先你的测试类应该继承AbstractTransactionalSpringContextTests这个类,覆盖他的一个方法;getConfigLocations

如下所示

public class ArticleDataDaoTest extends AbstractTransactionalSpringContextTests {
 protected String[] getConfigLocations() {
  return new String[] { "classpath:applicationContext.xml" };

 }

 CfmCatalogArticleDataDAO cfmCatalogArticleDataDAO;

 CfmCatalogMediaDAO media;

 public CfmCatalogMediaDAO getMedia() {
  return media;
 }

 public void setMedia(CfmCatalogMediaDAO media) {
  this.media = media;
 }
 public CfmCatalogArticleDataDAO getCfmCatalogArticleDataDAO() {
  return cfmCatalogArticleDataDAO;
 }

 public void setCfmCatalogArticleDataDAO(
   CfmCatalogArticleDataDAO cfmCatalogArticleDataDAO) {
  this.cfmCatalogArticleDataDAO = cfmCatalogArticleDataDAO;
 }

 public void testTreeView() {
  List list = cfmCatalogArticleDataDAO.getFormalListByCataTypeIdAndName(
    null, "01", -1, -1);
  assertTrue(list.size() != 0);
 }

}

 然后直接运行就可以了,在这个过程中涉及到的所有事务都回RollBack.(),我觉得这个功能不错。
**********************************************************************************************
对于service层的测试,由于我们在service层引用了Dao,而且service层配置了事务,呵呵,我的事务是声明式的。首先看事务的定义:
 

   <bean id="txProxyTemplate" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
      <property name="transactionManager">
        <ref local="myTransactionManager"/>
      </property>
      <property name="transactionAttributes">
        <props>
          <prop key="create*">PROPAGATION_REQUIRED,-com.finegold.digimus.exception.DigimusException</prop>
          <prop key="save*">PROPAGATION_REQUIRED,-com.finegold.digimus.exception.DigimusException</prop>
          <prop key="remove*">PROPAGATION_REQUIRED,-com.finegold.digimus.exception.DigimusException</prop>
          <prop key="update*">PROPAGATION_REQUIRED,-com.finegold.digimus.exception.DigimusException</prop>
          <prop key="del*">PROPAGATION_REQUIRED,-com.finegold.digimus.exception.DigimusException</prop>
          <prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
        </props>
      </property>
    </bean>

 事务管理器的定义:
   

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

 原来service层对事务的定义:

    <bean id="usersTarget" class="com.finegold.digimus.service.imp.UsersServiceImp">
       <property name="usersDAO">
          <ref local="SfmUsersDAO"/>
       </property>
    </bean>
    <bean id="usersService" parent="txProxyTemplate">
        <property name="target">
           <ref local="usersTarget"/>
        </property>
    </bean>

 问题就出在这里,如果是这样定义的话,测试的时候一直出错,错误如下,

org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'com.finegold.digimus.dao.imp.test.UserDaoTest': 
Unsatisfied dependency expressed through bean property 'userService':
 Set this property value or disable dependency checking for this bean.
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.checkDependencies(AbstractAutowireCapableBeanFactory.java:995)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:856)
 at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireBeanProperties(AbstractAutowireCapableBeanFactory.java:227)
 at org.springframework.test.AbstractDependencyInjectionSpringContextTests.setUp(AbstractDependencyInjectionSpringContextTests.java:201)
 at junit.framework.TestCase.runBare(TestCase.java:125)
 at junit.framework.TestResult$1.protect(TestResult.java:106)
 at junit.framework.TestResult.runProtected(TestResult.java:124)
 at junit.framework.TestResult.run(TestResult.java:109)
 at junit.framework.TestCase.run(TestCase.java:118)
 at junit.framework.TestSuite.runTest(TestSuite.java:208)
 at junit.framework.TestSuite.run(TestSuite.java:203)
 at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:128)
 at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386)
 at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196)

 
解决办法:将service层的bean定义改写为如下的方式:

    <bean id="usersService" parent="txProxyTemplate">
        <property name="target">
              <bean class="com.finegold.digimus.service.imp.UsersServiceImp">
       <property name="usersDAO"> <ref local="SfmUsersDAO"/></property>
        </bean>
        </property>
    </bean>

 ,然后就可以向dao一样,写service层的测试了,不过你要把相应的dao接口改为service的接口哦。
呵呵,好了,祝你成功!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值