你的位置:首页 > Java教程

[Java教程]Java_Activiti5_菜鸟也来学Activiti5工作流_之JUnit单元测试(四)


 1 /**ActivitiSpringJuinitTest.java 2  * author : 冯孟活 ^_^ 3  * dates : 2015年9月2日 下午2:16:54 4  * class  : activiti 之 Junit 测试 5 */ 6 // 指定运行环境要用到spring的测试包(spring-test.jar) 7 @RunWith(SpringJUnit4ClassRunner.class) 8 // 注入一些配置信息 9 @ContextConfiguration(locations="classpath:applicationConfig.) 10 public class ActivitiSpringJuinitTest {11   12   @Autowired // 注入运行服务类13   private RuntimeService runtimeService;14   15   @Autowired // 注入任务服务类16   private TaskService taskService;17   18   @Autowired19   @Rule // 注入一些规则20   private ActivitiRule activitiSpringRule;21   22   @Test 23   @Deployment // 部署24   public void activitiTest(){25     // 根据key来启动流程实例26     runtimeService.startProcessInstanceByKey("myProcess");27     // 获取单个任务28     Task task = taskService.createTaskQuery().singleResult();29     // 断言任务名称就是 "My Task"30     Assert.assertEquals("My Task",task.getName());31     // 完成任务32     taskService.complete(task.getName());33     /**34      * 总结:简单的单元测试到此为止!35      *       注意:药正确使用 注解类36      *           遇到的问题:37      *           Caused by: java.lang.ClassNotFoundException: org.junit.Assume$AssumptionViolatedException38      *           这一般是spring-text包与junit包冲突引起,把junit改低版本就没有问题了^_^39     */40   }41 }42 43 44 <!-- applicationConfig.45 <?46 <beans 47   48   49   50   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd51     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd52     http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">53   54   <!-- 配置数据源 -->55   <bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">56     <property name="driverClass" value="com.mysql.jdbc.Driver"/>57     <property name="url" value="jdbc:mysql://localhost:3306/db_activiti?useUnicode=true&amp;characterEncoding=utf-8"/>58     <property name="username" value="root"/>59     <property name="password" value="root"/>60   </bean>61 62   <!-- 配置数据源事务管理器 -->63   <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">64     <property name="dataSource" ref="dataSource"/> <!-- 引用上面的数据源 -->65   </bean>66   67   <!-- 配置流程引擎配置类 注意:这是用 org.activiti.spring.SpringProcessEngineConfiguration 这个类-->68   <bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">69     <property name="dataSource" ref="dataSource"/>70     <property name="transactionManager" ref="transactionManager" />71     <property name="databaseSchemaUpdate" value="true" />72     <property name="jobExecutorActivate" value="false" />73     <property name="createDiagramOnDeploy" value="false" /> <!-- 是否生成流程定义图片 -->74   </bean>75   76   <!-- 配置流程引擎工厂 -->77   <bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">78     <property name="processEngineConfiguration" ref="processEngineConfiguration" />79   </bean>80   81   <!-- 配置注入一些服务 -->82   <bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>83   <bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>84   <bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />85   <bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />86   <bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" />87   88   <!-- 配置activiti的规则 -->89   <bean id="activitiRule" class="org.activiti.engine.test.ActivitiRule">90     <property name="processEngine" ref="processEngine" />91   </bean>92   93 </beans>