查了半天的資料,終于搞清楚了如何在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的接口哦。
呵呵,好了,祝你成功!