JBoss.orgCommunity Documentation

Chapter 19.  Test Driven Development for Workflow

19.1. Introducing Test Driven Development for Workflow
19.2. XML Sources
19.2.1. Parsing a Process Archive
19.2.2. Parsing an XML File
19.2.3. Parsing an XML String
19.3. Testing sub processes

Since developing process-oriented programs is no different from developing any other software, Red Hat believes that one should be able to test process definitions easily. Read this chapter to learn how to use JUnit without any extensions to unit test custom process definitions.

Keep the development cycle as short as possible. Verify all changes to software source code immediately, (preferably, without any intermediate build steps.) The following examples demonstrate how to develop and test jBPM processes in this way.

Most process definition unit tests are execution-based. Each scenario is executed in one JUnit test method and this feeds the external triggers (signals) into a process execution. It then verifies after each signal to confirm that the process is in the expected state.

Here is an example graphical representation of such a test. It takes a simplified version of the auction process:


Next, write a test that executes the main scenario:

public class AuctionTest extends TestCase {


  // parse the process definition
  static ProcessDefinition auctionProcess = 
      ProcessDefinition.parseParResource("org/jbpm/tdd/auction.par");
  // get the nodes for easy asserting
  static StartState start = auctionProcess.getStartState();
  static State auction = (State) auctionProcess.getNode("auction");
  static EndState end = (EndState) auctionProcess.getNode("end");
  // the process instance
  ProcessInstance processInstance;
  // the main path of execution
  Token token;
  public void setUp() {
    // create a new process instance for the given process definition
    processInstance = new ProcessInstance(auctionProcess);
    // the main path of execution is the root token
    token = processInstance.getRootToken();
  }
  
  public void testMainScenario() {
    // after process instance creation, the main path of 
    // execution is positioned in the start state.
    assertSame(start, token.getNode());
    
    token.signal();
    
    // after the signal, the main path of execution has 
    // moved to the auction state
    assertSame(auction, token.getNode());
    
    token.signal();
    
    // after the signal, the main path of execution has 
    // moved to the end state and the process has ended
    assertSame(end, token.getNode());
    assertTrue(processInstance.hasEnded());
  }
}

Before writing execution scenarios, one must compose a ProcessDefinition. The easiest way to obtain a ProcessDefinition object is by parsing XML. With code completion switched on, type ProcessDefinition.parse. The various parsing methods will be displayed. There are three ways in which to write XML that can be parsed to a ProcessDefinition object:

TODO (see test/java/org/jbpm/graph/exe/ProcessStateTest.java)