Junit测试的mini流程helloworld
这是一个在demo中使用的Script Task做的简单示例,在执行到这个任务结点的时候自动输出"hello world"
打开BPMN2-MinimalProcess.bpmn2文件
流程如上图,流程开始的时候自动执行这个节点,然后在节点上执行输出的命令
看一看测试程序
public void testMinimalProcess() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-MinimalProcess.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ProcessInstance processInstance = ksession.startProcess("Minimal");
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
在执行完成后断言一下执行的结果为完成状态,就是完成状态是true
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
在控制台中成功输出结果
Loading process BPMN2-MinimalProcess.bpmn2
Hello World
表示示例加载运行成功
Junit测试的加载子流程的helloworld
这是一个在demo中使用子流程的示例,功能和上面一样但是用到了子流程
流程如下图:
调用程序如下:
public void testCompositeProcessWithDIGraphical() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-CompositeProcessWithDIGraphical.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ProcessInstance processInstance = ksession.startProcess("Composite");
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
完成之后状态如上,并且在控制台同样输出hello world
Junit测试script task
功能和上两个是一样的,截图
运行
public void testScriptTask() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-ScriptTask.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
ProcessInstance processInstance = ksession.startProcess("ScriptTask");
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
输出
Loading process BPMN2-ScriptTask.bpmn2
Hello World
Junit测试输出list
功能如上,但是在script task中输出列表
action属性的内容如下
运行输出
Loading process BPMN2-Import.bpmn2
[1]
Junit测试Rule Task
这次要加载的文件有两个,一个是BPMN2-RuleTask.bpmn2,一个是BPMN2-RuleTask.drl,一个是流程文件,一个是规则文件
里面加载的是Rule Task
并且设定规则为
然后看一下drools规则文件中的内容
package com.sample;
global java.util.List list;
rule MyRule
ruleflow-group "myRules"
when
then
list.add("Executed");
end
定义了规则组myRules,如果满足条件那条将加list中添中一条值
于是在我们的测试代码中就全看到
assertTrue(list.size() == 1);
看一下完整的测试代码
public void testRuleTask() throws Exception {
System.out.println("Loading process BPMN2-RuleTask.bpmn2");
KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
kbuilder.add(ResourceFactory.newClassPathResource("junit/BPMN2-RuleTask.bpmn2"), ResourceType.BPMN2);
kbuilder.add(ResourceFactory.newClassPathResource("junit/BPMN2-RuleTask.drl"), ResourceType.DRL);
KnowledgeBase kbase = kbuilder.newKnowledgeBase();
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
List< String > list = new ArrayList< String >();
ksession.setGlobal("list", list);
ProcessInstance processInstance = ksession.startProcess("RuleTask");
assertTrue(processInstance.getState() == ProcessInstance.STATE_ACTIVE);
ksession.fireAllRules();
assertTrue(list.size() == 1);
assertProcessInstanceCompleted(processInstance.getId(), ksession);
}
加入两个配置文件,并且传入list,当结点执行完成后,list中的值自动添加了一条。测试结果正常通过。
Junit测试数据对象
这个示例演示的是从外部传入数据,并且在内部得到数据并且显示的示例
其中Log节点是一个Script Task,它的Action属性里边设置了值employee,它的值是从外部传入的
System.out.println("Processing evaluation for employee " + employee);
看一下测试的代码
public void testDataObject() throws Exception {
KnowledgeBase kbase = createKnowledgeBase("BPMN2-DataObject.bpmn2");
StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);
Map< String , Object> params = new HashMap< String , Object>();
params.put("employee", "UserId-12345");
ProcessInstance processInstance = ksession.startProcess("Evaluation", params);
assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);
}
传入的employee的值是"UserId-12345"
运行完成后看输出的结果
Loading process BPMN2-DataObject.bpmn2
Processing evaluation for employee UserId-12345
显示我们传入的信息,这对我们做流程判断非常有用。
标签:ProcessInstance,ksession,list,介绍,processInstance,jbpm5.1,BPMN2,bpmn2 From: https://blog.51cto.com/u_4172728/5982937