Testing a sequence of nodes by using the evaluate() method

You can test a sequence of message flow nodes by using the evaluate() method.

Before you begin

Read the following topics:

About this task

You can test a sequence of message flow nodes by using the evaluate() method and setting the singleNodeTest parameter to false. When the singleNodeTest parameter is set to false, messages can be propagated from any of the output terminals on the node spy. The evaluate() method injects the message to the input terminal of a message flow node, and the flow continues either until the injected message reaches the end of the flow, or until an uncaught message-flow exception occurs.

When the evaluate() method is called with the singleNodeTest parameter set to false, the sequence of message flow nodes is executed synchronously on the unit test thread. The evaluate() method completes when evaluation of the sequence of nodes completes. If any of the evaluated nodes makes a request to other flows (such as a Callable Invoke node), invokes external APIs, or accesses external resources, these must be running and available in order for the evaluate() to complete successfully.

In addition to the singleNodeTest parameter, you can set two other properties on the evaluate() method:
  • The payload to be propagated.
  • Optionally, the name of the terminal to send the payload to. The terminal name (if specified) must be the name of an input terminal. If it is not specified, the 'in' terminal is used.
Figure 1. Example message flow
This diagram shows an example message flow containing HTTP input and reply nodes and two Compute nodes..

This example calls a message flow node by sending a message to the 'in' terminal of the Compute1 node. The HTTPReply node detects that there was no initial HTTP Input node in scope, so it does nothing. Other transport types might need extra code to prevent errors from being generated.


// Define the Spy on the Compute1 that is used to inject the message
SpyObjectReference compute1Reference = new SpyObjectReference().application("ExamplesApp").
    messageFlow("Example1").node("Compute1");
NodeSpy compute1NodeSpy = new NodeSpy(compute1Reference);

// Define the Spy on the Compute2 that 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 that 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 'in' terminal of the Compute1 node
compute1NodeSpy.evaluate(inputMessageAssembly, false, "in"); // Note: singleNodeTest = false 

// Assert the Compute1 node and Compute2 nodes are both called
assertThat(compute1NodeSpy, nodeCallCountIs(1));
assertThat(compute2NodeSpy, nodeCallCountIs(1));

// Assert the HTTP Reply node is also called
assertThat(httpReplyNodeSpy, nodeCallCountIs(1));
When you use the evaluate() method to test a section of a message flow, you can configure additional node spies and node stubs on the message flow to perform any of the following actions:
  • Observe and validate the message content at any node in the message flow
  • Prevent message propagation past a specific message flow node terminal, by using the setStopAtTerminal() method
  • Substitute the operation of a message flow node