Activiti单元测试

该博客介绍了如何进行Activiti单元测试的配置,包括在Maven项目中创建`src/test/java`和`src/test/resources`目录,将activiti.cfg.xml配置文件放入`test/resources`,并在同级目录下放置符合命名规则的流程文件,如测试类名.方法名.bpmn20.xml。此外,博主还分享了测试过程中遇到的问题及其解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

src/test/java

src/test/resources

二、在test/resources加入配置文件activiti.cfg.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans   http://www.springframework.org/schema/beans/spring-beans.xsd">

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

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

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

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

    <!-- mail server configurations -->
    <property name="mailServerPort" value="5025" />
    <property name="history" value="full" />
  </bean>

</beans>

三、测试类及流程文件

1、测试类

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



public class OneJobTest extends PluggableActivitiTestCase {
  
  @Deployment
  public void testTask() {
    try {
    	 runtimeService.startProcessInstanceByKey("testOneJobProcess");
        fail();
      } catch (Throwable e) {
        assertTrue(e instanceof NullPointerException);
      }
      
  }

import org.activiti.engine.delegate.JavaDelegate;
import org.activiti.engine.delegate.DelegateExecution;

public class TestOneJobDelegate implements JavaDelegate {
  
  public void execute(DelegateExecution execution) {
	  String s = null;
	  System.out.println(s.toString());
  }
  
}


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


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

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

<?xml version="1.0" encoding="UTF-8"?>
<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">
  <process id="testOneJobProcess" isExecutable="true">
    <startEvent id="theStart"></startEvent>
    <sequenceFlow id="flow1" sourceRef="theStart" targetRef="javaService"></sequenceFlow>
    <serviceTask id="javaService" name="Java service invocation" activiti:class="com.tuniu.nfbird.sample.test.delegate.TestOneJobDelegate"></serviceTask>
    <sequenceFlow id="flow2" sourceRef="javaService" targetRef="theEnd"></sequenceFlow>
    <endEvent id="theEnd"></endEvent>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_testOneJobProcess">
    <bpmndi:BPMNPlane bpmnElement="testOneJobProcess" id="BPMNPlane_testOneJobProcess">
      <bpmndi:BPMNShape bpmnElement="theStart" id="BPMNShape_theStart">
        <omgdc:Bounds height="35.0" width="35.0" x="170.0" y="205.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="javaService" id="BPMNShape_javaService">
        <omgdc:Bounds height="60.0" width="100.0" x="250.0" y="190.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="theEnd" id="BPMNShape_theEnd">
        <omgdc:Bounds height="35.0" width="35.0" x="400.0" y="205.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
        <omgdi:waypoint x="205.0" y="222.0"></omgdi:waypoint>
        <omgdi:waypoint x="212.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="212.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="250.0" y="220.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
        <omgdi:waypoint x="350.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="362.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="362.0" y="220.0"></omgdi:waypoint>
        <omgdi:waypoint x="400.0" y="222.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

四、引入依赖的jar包

	<dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.10</version>
            <scope>test</scope>
        </dependency>
        <dependency>
	      <groupId>com.h2database</groupId>
	      <artifactId>h2</artifactId>
	      <version>1.3.170</version>
	      <scope>test</scope>
	</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
       <dependency>
	      <groupId>com.h2database</groupId>
	      <artifactId>h2</artifactId>
	      <version>1.3.170</version>
	      <scope>test</scope>
	</dependency>

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

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






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值