最近一直在做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