Fluent Commerce Logo
Docs

Workflow Executor

Essential knowledge

Intended Audience:

Technical User

Author:

Kirill Gaiduk

Changed on:

5 Sept 2025

Overview

While the `RuleExecutor` is perfect for unit testing a single Rule, the `WorkflowExecutor` is the tool of choice for integration testing an entire Workflow or parts of it. It reads a Workflow definition from a JSON file and simulates the Fluent Orchestration Engine by executing the sequence of Rules and Rulesets.

Key points

  • Integration Testing Focus`WorkflowExecutor` is the primary tool for integration testing an entire or parts of a Workflow.
  • Workflow Simulation: It mimics the Fluent Commerce Orchestration Engine by processing an initial Event and then passing the resulting Event to the next Ruleset in the chain.
  • Rule Mocking: The `.mockRule()` method is a powerful feature that allows you to replace a real Rule in the Workflow with a mock implementation. This is crucial for isolating the part of the Workflow you want to test.
  • Targeted API Mocking: The `.mockNamedQuery()` method lets you mock a GraphQL query only when it is called by a specific Rule, giving you fine-grained control over test data for different stages of the Workflow.
  • Consolidated Assertions: The `execute` method returns a single Context containing a consolidated list of all Actions from all Rules, allowing you to assert the final state.

Core Methods

The `WorkflowExecutor` builds upon the capabilities of the previous `TestExecutor` and adds several new features:
`of`
This method automatically loads Workflows from a JSON file and includes Rules within the project.
`mockRule`
The `WorkflowExecutor` allows you to simulate Rule executions by specifying mock behaviors for Rules based on their names, bypassing the need for actual Rule execution.In other words, you can instruct the `WorkflowExecutor`: “Whenever a Rule with this name is encountered, skip the actual execution and simply assume it produced this Action.”
`mockNamedQuery`
In order to define specific mock API responses for each Rule identified by name you can use this method.After running the `WorkflowExecutor`, it returns a `RuleContextGenerator` that you can use to assert the expected outcomes of the Workflow execution.
`ignoreRules`
In order to ignore a specific set of rules during test execution, use the `WorkflowExecutor.ignoreRules` method.
`execute`
Use the `WorkflowExecutor.execute` method to execute the workflow and return the `TestContext` with any produced actions. This method runs the workflow with a default event that has no attributes and references an order entity.
`execute` (event)
Use the `WorkflowExecutor.execute` method with an event parameter to execute the workflow with a specific Event and return the `TestContext` with any actions that were produced. This version allows you to run the rule with custom event attributes and/or entity details.

Complete Example

Testing with `WorkflowExecutor` is similar to the `RuleExecutor` but is designed to test the interactions between rules.

1. Test Workflow

First, you need a workflow definition file in your `src/test/resources` directory (e.g., `workflows/my_order_workflow.json`). This file defines the sequence of rules that are triggered by specific events.

2. Test Class

The test class uses the `WorkflowExecutor` to load the workflow, mock any necessary dependencies, and execute it with an initial event.