期待更好更穩(wěn)定的開(kāi)源FrameWork的出現(xiàn),讓我們一起努力吧!  
          日歷
          <2008年10月>
          2829301234
          567891011
          12131415161718
          19202122232425
          2627282930311
          2345678
          統(tǒng)計(jì)
          • 隨筆 - 78
          • 文章 - 1
          • 評(píng)論 - 29
          • 引用 - 0

          導(dǎo)航

          常用鏈接

          留言簿(1)

          隨筆分類(lèi)

          隨筆檔案(42)

          文章檔案(37)

          相冊(cè)

          搜索

          •  

          積分與排名

          • 積分 - 45358
          • 排名 - 1063

          最新隨筆

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

           

          查了半天的資料,終于搞清楚了如何在hibernate+spring的框架下對(duì)DAO層和service層進(jìn)行測(cè)試了。我把自己的經(jīng)驗(yàn)總結(jié)一下,希望對(duì)大家有所幫助。
          最重要的是applicationContext.xml的配置,
          dao的配置如下
           
          <bean id="CfmStorageRecordDAO"
            class="com.finegold.digimus.dao.imp.CfmStorageRecordDAOImp">
            <property name="sessionFactory">
             <ref bean="sessionFactory" />
            </property>
           </bean>

          如果是這樣的話,對(duì)于dao的測(cè)試,我想應(yīng)該沒(méi)有什么問(wèn)題;
          首先你的測(cè)試類(lèi)應(yīng)該繼承AbstractTransactionalSpringContextTests這個(gè)類(lèi),覆蓋他的一個(gè)方法;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);
           }

          }
          然后直接運(yùn)行就可以了,在這個(gè)過(guò)程中涉及到的所有事務(wù)都回RollBack.(),我覺(jué)得這個(gè)功能不錯(cuò)。
          **********************************************************************************************
          對(duì)于service層的測(cè)試,由于我們?cè)趕ervice層引用了Dao,而且service層配置了事務(wù),呵呵,我的事務(wù)是聲明式的。首先看事務(wù)的定義:
              <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>
          事務(wù)管理器的定義:
              <bean id="myTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
                <property name="sessionFactory">
                  <ref local="sessionFactory"/>
                </property>
              </bean>
          原來(lái)service層對(duì)事務(wù)的定義:
              <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>
          問(wèn)題就出在這里,如果是這樣定義的話,測(cè)試的時(shí)候一直出錯(cuò),錯(cuò)誤如下,
          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定義改寫(xiě)為如下的方式:
              <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一樣,寫(xiě)service層的測(cè)試了,不過(guò)你要把相應(yīng)的dao接口改為service的接口哦。
          呵呵,好了,祝你成功!

          posted on 2007-06-22 17:43 BlueSky_itwangxinli 閱讀(5072) 評(píng)論(2)  編輯  收藏
          評(píng)論:
          • # re: Spring+hibernate dao和service層的單元測(cè)試  yupeng-raul7 Posted @ 2008-10-17 11:36
            樓主能不能發(fā)一份代碼給我啊,
            我現(xiàn)在正在做這個(gè),碰到麻煩了,謝謝!!
            我的郵箱:yupeng_raul7@hotmail.com
            謝謝!!!  回復(fù)  更多評(píng)論   

          • # re: Spring+hibernate dao和service層的單元測(cè)試[未登錄](méi)  阿飛 Posted @ 2011-07-17 15:41
            LZ,你好,我也遇到了這個(gè)問(wèn)題,不知道時(shí)隔這么多年,你是否還能幫我解決,我的Service層的bean設(shè)置跟你下面正確的配置是一樣的,但是還是報(bào)上述錯(cuò)誤,Unsatisfied dependency expressed through bean property transactionManager,我的qq:857622848,期待您的幫助~  回復(fù)  更多評(píng)論   


          只有注冊(cè)用戶(hù)登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
           
          Copyright © BlueSky_itwangxinli Powered by: 博客園 模板提供:滬江博客
          主站蜘蛛池模板: 怀仁县| 驻马店市| 正蓝旗| 冕宁县| 汉川市| 弥渡县| 桐城市| 从江县| 剑阁县| 九江市| 浪卡子县| 新宁县| 都江堰市| 墨竹工卡县| 永平县| 财经| 磐安县| 新宁县| 绥江县| 通山县| 湘乡市| 日照市| 即墨市| 连云港市| 新蔡县| 彰武县| 鲜城| 澎湖县| 景德镇市| 兰考县| 东台市| 黔东| 长寿区| 原平市| 黑水县| 鄂伦春自治旗| 临西县| 鄄城县| 眉山市| 肃南| 连南|