期待更好更穩定的開源FrameWork的出現,讓我們一起努力吧! |
|
|||
日歷
統計
導航常用鏈接留言簿(1)隨筆分類隨筆檔案(42)
文章檔案(37)
相冊搜索積分與排名
最新隨筆
最新評論
閱讀排行榜
評論排行榜
|
查了半天的資料,終于搞清楚了如何在hibernate+spring的框架下對DAO層和service層進行測試了。我把自己的經驗總結一下,希望對大家有所幫助。 如下所示 } CfmCatalogArticleDataDAO cfmCatalogArticleDataDAO; CfmCatalogMediaDAO media; public CfmCatalogMediaDAO getMedia() { public void setMedia(CfmCatalogMediaDAO media) { public void setCfmCatalogArticleDataDAO( public void testTreeView() { 然后直接運行就可以了,在這個過程中涉及到的所有事務都回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的接口哦。 呵呵,好了,祝你成功! |
![]() |
|
Copyright © BlueSky_itwangxinli | Powered by: 博客園 模板提供:滬江博客 |