Fluent Commerce Logo
Docs
Sign In
Essential knowledge

Author:

Kirill Gaiduk

Changed on:

24 June 2025

Overview

The `MockApiClient` is a critical component of the `util-test` library. It provides a mock implementation of the `ApiClient`, which is the interface Rules use to communicate with the Fluent Commerce GraphQL API.

By using the `MockApiClient`, you can test how your Rule behaves with different API responses without making any actual network calls. This isolates your tests, makes them faster, and allows you to simulate any scenario, including errors or empty data results.


Key points

  • Test API Interactions: The `MockApiClient` allows you to test how your rule responds to different API outcomes (e.g., success, failure, empty data) without making live network calls.
  • Isolate and Speed Up Tests: By mocking the API, your tests run faster and are isolated from network or environment issues.
  • Executor Integration: You typically don't use `MockApiClient` directly. You use the helper methods on `RuleExecutor` and `WorkflowExecutor` (like `.mockQuery()` and `.mockSetting()`) to configure it.
  • Full Coverage: It supports mocking pre-compiled queries/mutations, dynamic queries/mutations, and settings, giving you complete control over your test environment.

How It Works

You typically don't interact with the `MockApiClient` directly. Instead, you use the helper methods on the `RuleExecutor` or `WorkflowExecutor`, such as `.mockQuery()`, which configure the underlying mock client for you.

When your rule calls `context.api().query(...)` or `context.api().mutation(...)`, the mock client intercepts the call. Instead of making a network request, it looks for a predefined response that you have provided for that specific query or mutation. If it finds one, it returns the mock response; otherwise, it will typically return an empty result.

By default, the  `RuleExecutor`  and `WorkflowExecutor` automatically create a mock API client that the Rules under test use to perform queries.

However, if you need more control over the API behavior, you can manually create and configure a `MockApiClient` to simulate specific responses or conditions during your test.

1MockApiClient api = new MockApiClient();
2// now mock any responses to `api.query(GetSettingsQuery.builder().build())`
3api.mockNamedQuery(GetSettingsQuery.class, "graphql/settings/BasicSetting.json");