最近一直在做ssh的單元測試問題,數據庫是oracle,服務器是weblogic的,遇到了一些困難,查看了網上的一些資料,都寫的一言兩語,而且很多是一樣的,也不知道是誰抄襲的誰的,折騰了幾天終於搞出來,可以用於測試dao層和service層,在此做個節點
注意:下面是我遇到的一些問題
在我獲得到的新版本revenue中測試
1. 有些dao層的實現類中沒有使用@Repository注解(有多個)
這是加上注解后的
2. 有些service層中對要注入的dao的注解使用的不對
在JkBankAccountingManagerImpl.java中的使用是這樣的
如果報錯報出該類中的屬性jkDicBankAccountingDAO注入失敗時,采用上面的方法可以解決
不過大部分是不用加的
3. 我發現還有一些根本就用不到的java類,至少有2個,
像是UserSessionManagerImpl.java根本就沒有使用,當初始化spring的環境時,就會報錯
這樣可以暫時把該類注釋起來,或者做個備份刪掉
4. 還有一些實現類在spring初始化的時候會報出需要sessionFactory 或者hibernateTemplate
像是dicFunctionDAOImpl.java
可以暫時這樣處理:
<bean id="dicFunctionDAOImpl" class="com.revenue.admin.dao.impl.DicFunctionDAOImpl">
<property name="sessionFactory" ref="dicSessionFactory"></property>
</bean>
完整的測試service層用到的spring的
applicationContext-income-testservice.xml文件,在文件夾中可以找到

<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jee="http://www.springframework.org/schema/jee" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd"> <!-- 此處指定掃描 的包--> <context:component-scan base-package="com.revenue.income"></context:component-scan> <context:component-scan base-package="com.revenue.dic"></context:component-scan> <context:component-scan base-package="com.revenue.admin"></context:component-scan> <bean id="jacksonJSONObjectMapper" class="org.codehaus.jackson.map.ObjectMapper"></bean> <!-- 此處使用apache的東西直接連接oracle數據庫,這樣可以避開從weblogic環境中獲取income的數據源--> <bean id="incomeDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="username" value="income" /> <property name="password" value="income" /> <property name="initialSize" value="2" /> <property name="maxActive" value="15" /> <property name="testWhileIdle" value="true" /> <property name="validationQuery" value="select 1 from dual" /> <property name="testOnBorrow" value="true" /> </bean> <!-- 這是針對測試來說,新加入的事務管理組件 --> <bean id="transactionManager" class="org.springframework.transaction.jta.WebLogicJtaTransactionManager"> </bean> <bean id="incomeSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="incomeDataSource"></property> <property name="mappingDirectoryLocations"> <list> <value>classpath:/com/revenue/income/po/</value> <value>classpath:/com/revenue/dic/common/pojo/</value> <value>classpath:/com/revenue/dic/pte/pojo/</value> <value>classpath:/com/revenue/admin/pojo/</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect </prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.autoReconnect">true</prop> <prop key="hibernate.jdbc.fetch_size">100</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> </bean> <bean id="dicDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close"> <property name="driverClassName" value="oracle.jdbc.OracleDriver" /> <property name="url" value="jdbc:oracle:thin:@localhost:1521:orcl" /> <property name="username" value="dic" /> <property name="password" value="dic" /> <property name="initialSize" value="2" /> <property name="maxActive" value="15" /> <property name="testWhileIdle" value="true" /> <property name="validationQuery" value="select 1 from dual" /> <property name="testOnBorrow" value="true" /> </bean> <bean id="dicSessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource" ref="dicDataSource"></property> <!-- <property name="configLocation"> <value>classpath:hibernate.cfg.xml</value> </property> --> <property name="mappingDirectoryLocations"> <list> <value>classpath:/com/revenue/dic/common/pojo/</value> <value>classpath:/com/revenue/dic/pte/pojo/</value> <value>classpath:/com/revenue/admin/pojo/</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect"> org.hibernate.dialect.Oracle10gDialect </prop> <prop key="hibernate.show_sql">false</prop> <prop key="hibernate.format_sql">false</prop> <prop key="hibernate.autoReconnect">true</prop> <prop key="hibernate.jdbc.fetch_size">100</prop> <prop key="hibernate.jdbc.batch_size">20</prop> </props> </property> </bean> <!--暫時先這樣處理 --> <bean id="dicFunctionDAOImpl" class="com.revenue.admin.dao.impl.DicFunctionDAOImpl"> <property name="sessionFactory" ref="dicSessionFactory"></property> </bean> <bean id="jkDicBankAccountingDAOImpl" class="com.revenue.dic.pte.dao.impl.JkDicBankAccountingDAOImpl"> <property name="sessionFactory" ref="dicSessionFactory"></property> </bean> <bean id="dicUnitManagedDAOImpl" class="com.revenue.dic.common.dao.impl.DicUnitManagedDAOImpl"> <property name="sessionFactory" ref="dicSessionFactory"></property> </bean> <bean id="dicUserDAOImpl" class="com.revenue.admin.dao.impl.DicUserDAOImpl"> <property name="sessionFactory" ref="dicSessionFactory"></property> </bean> <bean id="vDicUnitDAOImpl" class="com.revenue.dic.common.dao.impl.VDicUnitDAOImpl"> <property name="sessionFactory" ref="dicSessionFactory"></property> </bean> <bean id="jkDicCustomerSubDAOImpl" class="com.revenue.dic.pte.dao.impl.JkDicCustomerSubDAOImpl"> <property name="sessionFactory" ref="dicSessionFactory"></property> </bean> <bean id="jkDicDebtTypeDAOImpl" class="com.revenue.dic.pte.dao.impl.JkDicDebtTypeDAOImpl"> <property name="sessionFactory" ref="dicSessionFactory"></property> </bean> <bean id="incomeJdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"> <property name="dataSource"> <ref bean="incomeDataSource" /> </property> </bean> <tx:advice id="incomeAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="get*" propagation="REQUIRED" read-only="true" /> <tx:method name="find*" propagation="REQUIRED" read-only="true" /> <tx:method name="load*" propagation="REQUIRED" read-only="true" /> <tx:method name="read*" propagation="REQUIRED" read-only="true" /> <tx:method name="list*" propagation="REQUIRED" read-only="true" /> <tx:method name="audit*" propagation="REQUIRED" timeout="3000" /> <tx:method name="check*" propagation="REQUIRED" timeout="3000" /> <tx:method name="*" propagation="REQUIRED" /> </tx:attributes> </tx:advice> <aop:config> <aop:pointcut id="incomeServiceOperation" expression="execution(* com.revenue.income.service.*.*(..))" /> <aop:advisor advice-ref="incomeAdvice" pointcut-ref="incomeServiceOperation" /> </aop:config> <!-- <aop:config proxy-target-class="true"> <aop:aspect> <aop:pointcut id="testManagerPointcut" expression="execution(* com.revenue.income.service.*.*(..))" /> <aop:advisor pointcut-ref="testManagerPointcut" advice-ref="methodProceedTimeInterceptor" /> </aop:aspect> </aop:config> <bean id="methodProceedTimeInterceptor" class="com.calculateprice.price.util.PriceMethodTimeAdvice" /> --> </beans>
5. 搭建junit測試環境:
把上面的jar包添加到項目的lib目錄中
7.模擬spring環境:
- 編寫測試用的spring的配置文件applicationContext-income-testservice.xml (文件內有說明)
- 存放位置:
可以放在src的conf目錄下,此時要在測試用例中使用
@ContextConfiguration(locations={ "classpath:conf/applicationContext-income-testservice.xml" }) 指定,
當然也可以隨意存放,只需在classpath指定的xml文件的路徑就可以
8. 新建測試用例
- 在項目中新建一個任意名字的source folder,如 junit_test
- 在創建一個package:如test,用於存放測試用例文件,如TestService.java
TestService.java的源代碼在給定的文件中可以找到

package test; import java.util.Date; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.test.annotation.Rollback; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.transaction.TransactionConfiguration; import org.springframework.transaction.annotation.Transactional; import com.revenue.income.po.JkDebt; import com.revenue.income.service.JkDebtManager; //表示使用自己定制的Junit4.5+運行器來運行測試,即完成Spring TestContext框架與Junit集成; @RunWith(SpringJUnit4ClassRunner.class) //指定要加載的Spring配置文件 @ContextConfiguration(locations = { "classpath:conf/applicationContext-income-testservice.xml" }) //這個非常關鍵,如果不加入這個注解配置,事務控制就會完全失效! @Transactional //開啟測試類的事務管理支持配置,並指定事務管理器和默認回滾行為 @TransactionConfiguration(transactionManager = "transactionManager", defaultRollback = true) public class TestService { @Autowired @Qualifier("jkDebtManagerImpl") private JkDebtManager jkDebtManager; @Before public void setUp() throws Exception { System.out.println("測試開始"); } @After public void tearDown() throws Exception { System.out.println("測試完畢"); } // @Test注解代表測試用例默認的測試方法,啟動時默認執行該方法 @Test @Rollback(false) // 表示該測試用例需要回滾 public void saveJkDebtTest() { JkDebt jkDebt = new JkDebt(); jkDebt.setUnit("hsp"); jkDebt.setNtype(206L); jkDebt.setNdate(new Date()); jkDebt.setCustomer("HSP2429"); jkDebt.setMoneyType(1L); jkDebt.setAmount(9999999.99); jkDebt.setStatus("正常"); jkDebtManager.save(jkDebt); System.out.println("保存成功"); } }
8. 運行測試用例文件:
這樣就不用啟動weblogic,直接在Test Service.java中 右擊 執行junit測試就可以了
輸出結果:
數據庫中的數據:
歡迎轉載,轉載時務必注明出處:http://www.cnblogs.com/wanggd