第 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());
}
}
15.2. XML 源 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
在编写执行场景前,您必须编写
ProcessDefinition。获取 ProcessDefinition 对象的最简单方法是解析 XML。当代码完成切换后,输入 ProcessDefinition.parse。此时会显示各种解析方法。可以通过两种方式将 XML 解析为 ProcessDefinition 对象:
15.2.1. 解析进程归档 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
进程存档 是一个 ZIP 文件,其中包含进程 XML 文件,即
处理definition.xml。lsblk Process Designer 插件读取和写入进程存档。
static ProcessDefinition auctionProcess =
ProcessDefinition.parseParResource("org/jbpm/tdd/auction.par");
15.2.2. 解析 XML 文件 复制链接链接已复制到粘贴板!
复制链接链接已复制到粘贴板!
要手动编写
processdefinition.xml 文件,请使用 JpdlXmlReader。使用 ant 脚本打包生成的 ZIP 文件。
static ProcessDefinition auctionProcess =
ProcessDefinition.parseXmlResource("org/jbpm/tdd/auction.xml");
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>");