JUnit Test Call sub-process
The following example tests the sub-flow of calling other programs in the program. Two configuration files need to be loaded.
And
Attribute needs to be specified
The following is a sample program called.
public void testCallActivity() throws Exception {System.out.println("Loading process BPMN2-CallActivity.bpmn2");KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();kbuilder.add(ResourceFactory.newClassPathResource("junit/BPMN2-CallActivity.bpmn2"),ResourceType.BPMN2);kbuilder.add(ResourceFactory.newClassPathResource("junit/BPMN2-CallActivitySubProcess.bpmn2"),ResourceType.BPMN2);KnowledgeBase kbase = kbuilder.newKnowledgeBase();StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);Map<String, Object> params = new HashMap<String, Object>();params.put("x", "oldValue");ProcessInstance processInstance = ksession.startProcess("ParentProcess", params);assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);assertEquals("new value",((WorkflowProcessInstance) processInstance).getVariable("y"));}
The program outputs the following results
Loading Process BPMN2-CallActivity.bpmn2
Subx = oldvalue
Verify that sub-process call is successful
Events triggered during the execution of the JUnit test sub-process
In the following example, various events are triggered during sub-process execution.
The process is as follows:
When hello1 is executed, the variable X has no value. When hello2 is executed, the value is assigned. When hello3 is executed, the variable can output the value.
The test procedure is as follows:
Public void testsubprocess () throws exception {Knowledgebase kbase = createknowledgebase ("BPMN2-SubProcess.bpmn2"); statefulknowledgesession ksession = createknowledgesession (kbase); ksession. addeventlistener (New defaultprocesseventlistener () {public void afterprocessstarted (processstartedevent event) {system. out. println ("after the process is started" + Event);} public void beforevariablechanged (processvariablechangedevent event) {system. out. println ("before the variable changes" + Event);} public void aftervariablechanged (processvariablechangedevent event) {system. out. println ("after the variable changes" + Event) ;}}); processinstance = ksession. startprocess ("subprocess"); asserttrue (processinstance. getstate () = processinstance. state_completed );}
The output result is as follows:
Loading Process BPMN2-SubProcess.bpmn2
X = NULL
Before changing the variable, ==> [processvariablechanged (ID = 2: X; instanceid = 1: X; oldvalue = NULL; newvalue = Hello; processname = minimal subprocess; processid = subprocess)]
After the variable changes, ==> [processvariablechanged (ID = 2: X; instanceid = 1: X; oldvalue = NULL; newvalue = Hello; processname = minimal subprocess; processid = subprocess)]
X = Hello
Goodbye World
After the process is started ==> [processstarted (name = minimal subprocess; id = subprocess)]
JUnit testing multi-Process Loop
The following example mainly tests the example of multi-process loop.
The interface process is as follows:
The test procedure is as follows:
public void testMultiInstanceLoopCharacteristicsProcess() throws Exception {KnowledgeBase kbase = createKnowledgeBase("BPMN2-MultiInstanceLoopCharacteristicsProcess.bpmn2");StatefulKnowledgeSession ksession = createKnowledgeSession(kbase);Map<String, Object> params = new HashMap<String, Object>();List<String> myList = new ArrayList<String>();myList.add("First Item");myList.add("Second Item");params.put("list", myList);ProcessInstance processInstance = ksession.startProcess("MultiInstanceLoopCharacteristicsProcess", params);assertTrue(processInstance.getState() == ProcessInstance.STATE_COMPLETED);}
The execution result is as follows:
Loading Process BPMN2-MultiInstanceLoopCharacteristicsProcess.bpmn2
Item = first item
Item = second item
Goodbye World
JUnit test abnormal event handling process
The following example mainly tests the processing process when exceptions are executed in the sub-process.
The procedure is as follows: