Unit Testing of jBPM Process Flows
Learn more about how to run unit tests with jBPM process flows.
Join the DZone community and get the full member experience.
Join For FreeUnit testing is an important step in the software development life cycle to ensure that your product works the way it is intended to work. Back in the day, it used to be just a step that developers had to perform to evaluate their work. In today’s world, it has become an essential part of the CI/CD pipeline as it is not just used to determine quality based on functionality but also to determine code promotion eligibility based on various numbers (pass/fail ratio or code coverage percentage).
jBPM, being one of the most modern, open-source bpm products, provides rich capabilities for unit testing BPM processes. This allows jBPM processes to be treated just like any other Java-based artifact. The jBPM tool kit provides an extension of JUnit test classes that allow one to easily unit test business processes defined in BPMN2.
We will look at a sample implementation where we use out-of-the-box unit testing capabilities provided by jBPM. jBPM provides a test module (jbpm-test.jar) that includes all of the classes needed for creating jBPM unit tests. It is easy to include the jbpm-test module to your jBPM project. You just need to add the dependency to your pom.xml.
<dependency>
<groupId>org.jbpm</groupId>
<artifactId>jbpm-test</artifactId>
<version>7.17.0.Final</version>
</dependency>
The jBPM test module provides the JbpmJUnitBaseTestCase
class, which provides various method implementations to support BPMN2 process unit testing .
JbpmJUnitBaseTestCase
class provides support in four major areas. Some of the most important ones in those areas are listed below:
JUnit Life Cycle Methods
setup()
: This is an@Before
annotated method to createEntityManagerFactory
and cleans up the Singleton's sessionIDteardown()
: This is an@After
annotated method, which clears out history, closesEntityManagerFactory
and the data source, and disposes of theRuntimeEngines
andRuntimeManager
.
KIE Session Management Methods
createRuntimeManager()
disposeRuntimeManager()
getRuntimeEngine()
Assertions
assertProcessInstanceCompleted
assertProcessInstanceActive
assertNodeTriggered
assertProcessVarExists
Helper Methods
getTestWorkItemHandler()
— returns test work item handler that might be registered in addition to what is registered by defaultsetupPoolingDataSource()
— sets up the data source
Below is a sample implementation using the JbpmJUnitBaseTestCase
.
public class HelloWorldTest extends JbpmJUnitBaseTestCase {
public HelloWorldTest() {
// This helps to setup data source and session persistence
super(true, true);
}
@Test
public void testHelloWorld() {
createRuntimeManager("com/sample/HelloWorld.bpmn2");
RuntimeEngine runtimeEngine = getRuntimeEngine();
KieSession ksession = runtimeEngine.getKieSession();
ProcessInstance processInstance = ksession.startProcess("com.sample.HelloWorld");
assertProcessInstanceCompleted(processInstance.getId());
disposeRuntimeManager();
}
}
This approach allows us to test basic process status and its life cycle. This is with the assumption that a process is high level and doesn’t have too many implementation logic embedded in the process. Currently, there is no way to measure the code coverage and branch in BPM processes. Hopefully it will come in future versions.
Opinions expressed by DZone contributors are their own.
Comments