关注JEECG发展历程 关注最新动态和版本, 记录JEECG成长点滴 更新日志 - 技术支持 - 招聘英才

JEECG最新版本下载 JEECG智能开发平台 - 显著提高开发效率 常见问题 - 入门视频 - 参与开源团队

商务QQ: 69893005、3102411850 商务热线(5*8小时): 010-64808099 官方邮箱: jeecgos@163.com

查看: 11689|回复: 0

Activiti单元测试

[复制链接]
发表于 2014-8-1 12:57:05 | 显示全部楼层 |阅读模式

前提:使用Maven进行依赖管理

一、如果没有,则创建文件夹:

src/test/java

src/test/resources

二、在test/resources加入配置文件activiti.cfg.xml

  1. <?xml version="1.0" encoding="UTF-8"?>

  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.   xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

  5.   <bean id="processEngineConfiguration"
  6.     class="org.activiti.engine.impl.cfg.StandaloneProcessEngineConfiguration">

  7.     <property name="jdbcUrl" value="jdbc:h2:mem:activiti;DB_CLOSE_DELAY=1000" />
  8.     <property name="jdbcDriver" value="org.h2.Driver" />
  9.     <property name="jdbcUsername" value="sa" />
  10.     <property name="jdbcPassword" value="" />

  11.     <!-- Database configurations -->
  12.     <property name="databaseSchemaUpdate" value="drop-create" />

  13.     <!-- job executor configurations -->
  14.     <property name="jobExecutorActivate" value="false" />

  15.     <!-- mail server configurations -->
  16.     <property name="mailServerPort" value="5025" />
  17.     <property name="history" value="full" />
  18.   </bean>

  19. </beans>
复制代码


三、测试类及流程文件


1、测试类

  1. import org.activiti.engine.impl.test.PluggableActivitiTestCase;
  2. import org.activiti.engine.test.Deployment;



  3. public class OneJobTest extends PluggableActivitiTestCase {
  4.   
  5.   @Deployment
  6.   public void testTask() {
  7.     try {
  8.              runtimeService.startProcessInstanceByKey("testOneJobProcess");
  9.         fail();
  10.       } catch (Throwable e) {
  11.         assertTrue(e instanceof NullPointerException);
  12.       }
  13.       
  14.   }
复制代码
  1. import org.activiti.engine.delegate.JavaDelegate;
  2. import org.activiti.engine.delegate.DelegateExecution;

  3. public class TestOneJobDelegate implements JavaDelegate {
  4.   
  5.   public void execute(DelegateExecution execution) {
  6.           String s = null;
  7.           System.out.println(s.toString());
  8.   }
  9.   
  10. }
复制代码

2、流程文件OneJobTest.testTask.bpmn20.xml


放在resources中于测试类同样的目录层次中。

命名规则为测试类名.方法名.bpmn20.xml。



  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:activiti="http://activiti.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="Examples">
  3.   <process id="testOneJobProcess" isExecutable="true">
  4.     <startEvent id="theStart"></startEvent>
  5.     <sequenceFlow id="flow1" sourceRef="theStart" targetRef="javaService"></sequenceFlow>
  6.     <serviceTask id="javaService" name="Java service invocation" activiti:class="com.tuniu.nfbird.sample.test.delegate.TestOneJobDelegate"></serviceTask>
  7.     <sequenceFlow id="flow2" sourceRef="javaService" targetRef="theEnd"></sequenceFlow>
  8.     <endEvent id="theEnd"></endEvent>
  9.   </process>
  10.   <bpmndi:BPMNDiagram id="BPMNDiagram_testOneJobProcess">
  11.     <bpmndi:BPMNPlane bpmnElement="testOneJobProcess" id="BPMNPlane_testOneJobProcess">
  12.       <bpmndi:BPMNShape bpmnElement="theStart" id="BPMNShape_theStart">
  13.         <omgdc:Bounds height="35.0" width="35.0" x="170.0" y="205.0"></omgdc:Bounds>
  14.       </bpmndi:BPMNShape>
  15.       <bpmndi:BPMNShape bpmnElement="javaService" id="BPMNShape_javaService">
  16.         <omgdc:Bounds height="60.0" width="100.0" x="250.0" y="190.0"></omgdc:Bounds>
  17.       </bpmndi:BPMNShape>
  18.       <bpmndi:BPMNShape bpmnElement="theEnd" id="BPMNShape_theEnd">
  19.         <omgdc:Bounds height="35.0" width="35.0" x="400.0" y="205.0"></omgdc:Bounds>
  20.       </bpmndi:BPMNShape>
  21.       <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
  22.         <omgdi:waypoint x="205.0" y="222.0"></omgdi:waypoint>
  23.         <omgdi:waypoint x="212.0" y="220.0"></omgdi:waypoint>
  24.         <omgdi:waypoint x="212.0" y="220.0"></omgdi:waypoint>
  25.         <omgdi:waypoint x="250.0" y="220.0"></omgdi:waypoint>
  26.       </bpmndi:BPMNEdge>
  27.       <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
  28.         <omgdi:waypoint x="350.0" y="220.0"></omgdi:waypoint>
  29.         <omgdi:waypoint x="362.0" y="220.0"></omgdi:waypoint>
  30.         <omgdi:waypoint x="362.0" y="220.0"></omgdi:waypoint>
  31.         <omgdi:waypoint x="400.0" y="222.0"></omgdi:waypoint>
  32.       </bpmndi:BPMNEdge>
  33.     </bpmndi:BPMNPlane>
  34.   </bpmndi:BPMNDiagram>
  35. </definitions>
复制代码


四、引入依赖的jar包

  1. <dependency>
  2.             <groupId>junit</groupId>
  3.             <artifactId>junit</artifactId>
  4.             <version>4.10</version>
  5.             <scope>test</scope>
  6.         </dependency>
  7.         <dependency>
  8.               <groupId>com.h2database</groupId>
  9.               <artifactId>h2</artifactId>
  10.               <version>1.3.170</version>
  11.               <scope>test</scope>
  12.         </dependency>
复制代码

OK, 设置完毕。


附:

遇到的问题及解决办法:


1、[main] ERROR o.a.e.i.c.ProcessEngineConfigurationImpl - Exception while initializing Database connection
java.sql.SQLException: Error setting driver on UnpooledDataSource. Cause: java.lang.ClassNotFoundException: org.h2.Driver
解决办法: 添加jar
  1.        <dependency>
  2.               <groupId>com.h2database</groupId>
  3.               <artifactId>h2</artifactId>
  4.               <version>1.3.170</version>
  5.               <scope>test</scope>
  6.         </dependency>
复制代码

2、eclipse maven报错:An error occurred while filtering resources 解决办法:点击Maven -> Update Projectj即可解决问题


3、运行junit后,报:java.lang.ClassNotFoundException解决办法:使用eclise做为ide,则要正确的设置web deploy assembly.

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

快速回复 返回顶部 返回列表