第15章 Workflow のテスト駆動開発 (TDD)

15.1. Workflow のテスト駆動開発の紹介

プロセス指向プログラムの開発は、 他のソフトウェアの開発と何ら変わりはないので、簡単にプロセス定義をテストできるべきだと考えます。この章では、JUnit の利用し拡張なしにカスタムのプロセス定義を単体テストする方法を見ていきます。
開発サイクルはできる限り短く保つべきです。(できれば中間の構築ステップを踏まず)ソフトウェアのソースコードへの変更をすべてすぐ検証してください。以下の例では、この方法を用いて JBPM プロセスを開発、テストする方法をお見せします。
プロセス定義の単体テストのほとんどは、実行ベースです。 各シナリオは、1つの JUnit テストメソッドで実行され、外部トリガー(シグナル) でプロセス実行に送り込んで、各シグナルの後にプロセスが期待された状態にあるかを検証します。
これは、そのようなテストのサンプルをグラフ表示したものです。 オークションプロセスの簡単なバージョンを取り上げます。
オークションテストプロセス

図15.1 オークションテストプロセス

次に、メインシナリオを実行するテストを記述してみましょう。
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());
  }
}