/**ActivitiSpringJuinitTest.java
* author : 冯孟活 ^_^
* dates : 2015年9月2日 下午2:16:54
* class : activiti 之 Junit 测试
*/
// 指定运行环境要用到spring的测试包(spring-test.jar)
@RunWith(SpringJUnit4ClassRunner.class)
// 注入一些配置信息
@ContextConfiguration(locations="classpath:applicationConfig.xml")
public class ActivitiSpringJuinitTest { @Autowired // 注入运行服务类
private RuntimeService runtimeService; @Autowired // 注入任务服务类
private TaskService taskService; @Autowired
@Rule // 注入一些规则
private ActivitiRule activitiSpringRule; @Test
@Deployment // 部署
public void activitiTest(){
// 根据key来启动流程实例
runtimeService.startProcessInstanceByKey("myProcess");
// 获取单个任务
Task task = taskService.createTaskQuery().singleResult();
// 断言任务名称就是 "My Task"
Assert.assertEquals("My Task",task.getName());
// 完成任务
taskService.complete(task.getName());
/**
* 总结:简单的单元测试到此为止!
* 注意:药正确使用 注解类
* 遇到的问题:
* Caused by: java.lang.ClassNotFoundException: org.junit.Assume$AssumptionViolatedException
* 这一般是spring-text包与junit包冲突引起,把junit改低版本就没有问题了^_^
*/
}
} <!-- applicationConfig.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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd"> <!-- 配置数据源 -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.SimpleDriverDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/db_activiti?useUnicode=true&characterEncoding=utf-8"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean> <!-- 配置数据源事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/> <!-- 引用上面的数据源 -->
</bean> <!-- 配置流程引擎配置类 注意:这是用 org.activiti.spring.SpringProcessEngineConfiguration 这个类-->
<bean id="processEngineConfiguration" class="org.activiti.spring.SpringProcessEngineConfiguration">
<property name="dataSource" ref="dataSource"/>
<property name="transactionManager" ref="transactionManager" />
<property name="databaseSchemaUpdate" value="true" />
<property name="jobExecutorActivate" value="false" />
<property name="createDiagramOnDeploy" value="false" /> <!-- 是否生成流程定义图片 -->
</bean> <!-- 配置流程引擎工厂 -->
<bean id="processEngine" class="org.activiti.spring.ProcessEngineFactoryBean">
<property name="processEngineConfiguration" ref="processEngineConfiguration" />
</bean> <!-- 配置注入一些服务 -->
<bean id="repositoryService" factory-bean="processEngine" factory-method="getRepositoryService"/>
<bean id="runtimeService" factory-bean="processEngine" factory-method="getRuntimeService"/>
<bean id="taskService" factory-bean="processEngine" factory-method="getTaskService" />
<bean id="historyService" factory-bean="processEngine" factory-method="getHistoryService" />
<bean id="managementService" factory-bean="processEngine" factory-method="getManagementService" /> <!-- 配置activiti的规则 -->
<bean id="activitiRule" class="org.activiti.engine.test.ActivitiRule">
<property name="processEngine" ref="processEngine" />
</bean> </beans>