使用 propagate() 方法测试一系列节点
可以使用 propagate() 方法来测试一系列消息流节点。
准备工作
关于此任务
可以使用 propagate() 方法来测试消息流节点的序列,该方法允许将消息注入消息流节点的输出终端。 使用 propagate() 方法时,不会调用消息流节点本身,但是会将消息从节点的输出终端向流下游传播。
调用 propagate() 方法时,将在单元测试线程上同步执行一系列节点。 当这一系列节点的评估完成后,propagate() 方法就完成了。 如果所评估的任何节点对其他流(例如可调用的“调用”节点)发出请求、调用外部 API 或访问外部资源,那么这些流必须正在运行且可用,传播才能成功完成。

此示例将消息传播到 HTTPInput 节点的“out”终端。 HTTPReply 节点检测到没有初始 HTTPInput 节点,所以它不会执行任何操作。 其他传输可能需要额外的代码以防止生成错误。
// Define the Spy on the HTTP Input which is used to inject the message
SpyObjectReference httpInputReference = new SpyObjectReference().application("ExamplesApp").
messageFlow("Example1").node("HTTP Input");
NodeSpy httpInputNodeSpy = new NodeSpy(httpInputReference);
// Define the Spy on the Compute2 which is used to verify the required nodes were executed
SpyObjectReference compute2Reference = new SpyObjectReference().application("ExamplesApp").
messageFlow("Example1").node("Compute2");
NodeSpy compute2NodeSpy = new NodeSpy(compute2Reference);
// Define the Spy on the HTTP Reply which is used to verify the operation
SpyObjectReference httpReplyReference = new SpyObjectReference().application("ExamplesApp").
messageFlow("Example1").node("HTTP Reply");
NodeSpy httpReplyNodeSpy = new NodeSpy(httpReplyReference);
// Declare a new TestMessageAssembly object for the message being sent into the node
TestMessageAssembly inputMessageAssembly = new TestMessageAssembly();
/*
* Loading of message assembly would be done here
*/
// Send the message assembly to the 'out' terminal of the HTTP Input node
httpInputNodeSpy.propagate(inputMessageAssembly, "out");
// Assert the HttpInput node was not called, because we injected the message at the
// output terminal using propagate
assertThat(httpInputNodeSpy, nodeCallCountIs(0));
// Assert the Compute2 node is called
assertThat(compute2NodeSpy, nodeCallCountIs(1));
// Assert the HTTP Reply node is called
assertThat(httpReplyNodeSpy, nodeCallCountIs(1));
使用
propagate() 方法来测试消息流的某个部分时,可以在消息流上配置其他节点间谍和节点存根,以执行以下任何操作:- 观察并验证消息流中任何节点处的消息内容
- 通过使用
setStopAtTerminal()方法,防止消息传播到特定的消息流节点终端 - 替换消息流节点的操作