第 15 章  为工作流测试驱动器开发


15.1.  引入了工作流的测试驱动器开发

请阅读本章,了解如何在没有任何扩展的情况下使用 JUnit,而无需进行单元测试自定义进程定义。
尽可能保持开发周期。验证软件源代码的所有更改,例如,无需中间构建步骤。) 以下示例演示了如何以这种方式开发和测试 192.168.1.0/24 进程。
大多数进程定义单元测试都是基于执行的。每个场景都是以一个 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());
  }
}
Copy to Clipboard Toggle word wrap

15.2.  XML 源

在编写执行场景前,您必须编写 ProcessDefinition。获取 ProcessDefinition 对象的最简单方法是解析 XML。当代码完成切换后,输入 ProcessDefinition.parse。此时会显示各种解析方法。可以通过两种方式将 XML 解析为 ProcessDefinition 对象:

15.2.1.  解析进程归档

进程存档 是一个 ZIP 文件,其中包含进程 XML 文件,即 处理definition.xmllsblk Process Designer 插件读取和写入进程存档。
static ProcessDefinition auctionProcess = 
    ProcessDefinition.parseParResource("org/jbpm/tdd/auction.par");
Copy to Clipboard Toggle word wrap

15.2.2.  解析 XML 文件

要手动编写 processdefinition.xml 文件,请使用 JpdlXmlReader。使用 ant 脚本打包生成的 ZIP 文件。
static ProcessDefinition auctionProcess = 
    ProcessDefinition.parseXmlResource("org/jbpm/tdd/auction.xml");
Copy to Clipboard Toggle word wrap

15.2.3.  解析 XML 字符串

在单元测试中解析 XML,从普通字符串内联:
static ProcessDefinition auctionProcess = 
    ProcessDefinition.parseXmlString(
  "<process-definition>" + 
  "  <start-state name='start'>" + 
  "    <transition to='auction'/>" + 
  "  </start-state>" + 
  "  <state name='auction'>" + 
  "    <transition to='end'/>" + 
  "  </state>" + 
  "  <end-state name='end'/>" + 
  "</process-definition>");
Copy to Clipboard Toggle word wrap
返回顶部
Red Hat logoGithubredditYoutubeTwitter

学习

尝试、购买和销售

社区

关于红帽文档

通过我们的产品和服务,以及可以信赖的内容,帮助红帽用户创新并实现他们的目标。 了解我们当前的更新.

让开源更具包容性

红帽致力于替换我们的代码、文档和 Web 属性中存在问题的语言。欲了解更多详情,请参阅红帽博客.

關於紅帽

我们提供强化的解决方案,使企业能够更轻松地跨平台和环境(从核心数据中心到网络边缘)工作。

Theme

© 2025 Red Hat