admin 发表于 2014-8-1 12:57:05

Activiti单元测试

前提:使用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.xmlhttp://img.blog.csdn.net/20140423202631437?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVhbmdjYW5nYmFp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center
放在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、 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.http://img.blog.csdn.net/20140511093019640?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvaHVhbmdjYW5nYmFp/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center

页: [1]
查看完整版本: Activiti单元测试