Class ScenarioTemplateHelper

java.lang.Object
org.mockserver.templates.engine.helpers.ScenarioTemplateHelper

public class ScenarioTemplateHelper extends Object
Template helper that exposes scenario state (the WireMock "scenarios" pattern) to response templates, so a value captured or derived in one request can drive a later response.

Exposed in all three template engines (Velocity, JavaScript, Mustache) under the scenario key (see TemplateFunctions.BUILT_IN_HELPERS).

Template API:

  • $scenario.get("name") — current state of scenario name ("Started" if never set, "" for a null name)
  • $scenario.set("name", "state") — updates the state and returns "" so it can be called inline in Velocity (e.g. $scenario.set('flow','step2'))
  • $scenario.matches("name", "state")true if the current state equals state

The helper is backed by the live ScenarioManager wired into CrossProtocolEventBus.getInstance() by HttpState — the same instance that matchers use. So a set from a template is immediately visible to a subsequent matcher matchesState check and vice-versa. The instance is resolved lazily on each call (rather than captured at construction) because TemplateFunctions#BUILT_IN_HELPERS is a static singleton created before any server is initialised; lazy resolution lets a single helper instance track whichever server is currently running.

Thread-safety: this helper is stateless and shared across all template executions. ScenarioManager is itself concurrency-safe (its state store is backed by a ConcurrentHashMap with compare-and-set transitions), so no additional synchronisation is required here.

Limitation: when no server has been initialised (e.g. the template engine is exercised in isolation with no HttpState), there is no live ScenarioManager; in that case get/matches return the unset defaults and set is a no-op rather than throwing.

  • Constructor Details

    • ScenarioTemplateHelper

      public ScenarioTemplateHelper()
  • Method Details

    • get

      public String get(String name)
      Returns the current state of the named scenario, or ScenarioManager.STARTED ("Started") if the scenario has never been set. Returns "" for a null name or when no live ScenarioManager is available.
    • set

      public String set(String name, String state)
      Updates the state of the named scenario. Returns "" so it can be invoked inline within a Velocity template (e.g. $scenario.set('flow','step2')) without emitting any output. No-op when no live ScenarioManager is available, or when name/state is null.
    • matches

      public boolean matches(String name, String state)
      Returns true if the current state of the named scenario equals state. Mirrors matcher semantics: an unset scenario is implicitly in the ScenarioManager.STARTED state. Returns false when no live ScenarioManager is available (except where state matches the implicit "Started" default).
    • toString

      public String toString()
      Overrides:
      toString in class Object