Chapter 8. Testing

Even though business processes should not be viewed as code, should be as high-level as possible and should not contain implementation details, they also have a life cycle just like other development artefacts. Therefore testing you Process definitions is just as important as it is when programming.

8.1. Unit testing

When unit testing your Process, you test whether the process behaves as expected in specific use cases; for example, you test the output based on the existing input. To simplify unit testing, the helper class org.jbpm.test.JbpmJUnitBaseTestCase is provided in the jbpm-bpmn2 test module that offers the following:
  • helper methods to create a new kie base and session for given processes (Also, you can select if persistence is to be used.)
  • assert statements to check among other also the following:
    • the state of a process instance (active, completed, aborted)
    • which node instances are currently active
    • which nodes have been triggered to check the path that has been followed
    • the value of variables

Example 8.1. JUnit test of the com.sample.bpmn.hello Process

  import org.kie.api.runtime.KieSession;
  import org.kie.api.runtime.manager.RuntimeEngine;
  import org.kie.api.runtime.process.ProcessInstance;
  
  public class MyProcessTest extends org.jbpm.test.JbpmJUnitBaseTestCase {
  
    public void testProcess() {

      // create singleton runtime manager and load the given process(es)
      createRuntimeManager("sample.bpmn");
      
      // get the single kie session
      RuntimeEngine engine = getRuntimeEngine();
      KieSession ksession = engine.getKieSession();
      
      // start the process
      ProcessInstance processInstance =
      ksession.startProcess("com.sample.bpmn.hello");
      
      // check whether the process instance has completed successfully
     assertProcessInstanceCompleted(processInstance.getId(), ksession);
     
      // check whether the given nodes were executed during the process execution
     assertNodeTriggered(processInstance.getId(), "StartProcess", "Hello", "EndProcess");
     
      }
}
The JUnit test will create a new session, start the com.sample.bpmn.hello process and verify whether the Process instance completed successfully and whether the nodes StartProcess, Hello, EndProcess have been executed.