Wrapper setup is the most complex to set up and the hardest to maintain of the four styles — you build the whole Java layer and wrap it in Cucumber steps that you then have to keep in sync.
So why use it? It's the best choice when many people contribute to the tests — product owners, manual QAs, non-developers with no testing background. You (a developer) write reusable, plain-English step definitions once; everyone else composes readable scenarios out of them, in the language of the domain, and they execute against your Java. It trades setup/maintenance cost for approachability at scale.
Wrapper setup is the custom-script style with a Cucumber layer on top. So the first half of the work is identical: set up (and reuse) the same building blocks. Follow the custom script guide §1–§4:
- Base URL — config loader manual
- Request/response DTOs — request & response body DTO manual
- Clients — clients manual
- Flows (optional) — flows manual
If you already have these from a script-style test, you reuse them as-is — the wrapper just calls the same clients/flows from inside step definitions.
The extra half is: expose those calls as Gherkin steps. Create your step definition classes anywhere under src/test/java/api/tests (the glue root, configured in BddTestRunner). A step definition is a Java method annotated with a step phrase; a feature file then uses that phrase.
The working examples:
| Feature (what non-devs write) | Step definitions (what you write) |
|---|---|
| crudUserTest.feature | UserCrudStepDef |
| userProfileTest.feature | UserProfileStepDef |
Each step method just calls a client/flow and stores the result for the next step (see §3–§4 for how that "storing" works).
A scenario's steps usually span multiple step-definition classes (e.g. one for CRUD, one for auth). They need to share data — the response from step 1 must reach step 2. That sharing is done with dependency injection, and here we use Cucumber PicoContainer.
How it works: when a scenario runs, Cucumber (via PicoContainer) creates the step-definition objects and injects a shared context instance into each one's constructor. Every step class in that scenario receives the same instance, so whatever one step stores, the next can read.
public final class UserCrudStepDef {
private final ScenarioContext context;
public UserCrudStepDef(ScenarioContext context) { // PicoContainer injects this
this.context = context;
}
}You never construct it yourself — declare it as a constructor parameter and Cucumber provides it.
The thing injected above is a ScenarioContext: a per-scenario key–value store. PicoContainer creates one per scenario and shares it across all step classes in that scenario, so it's how steps hand data to one another; it's cleared between scenarios, keeping them isolated.
Its methods:
context.setContext(CoreContextKey.FLUENT_RESPONSE, fluentResponse); // store by typed key (preferred)
context.setContext("storedEmail", "a@b.com"); // store by string key
FluentResponse<?> r = context.getContext(CoreContextKey.FLUENT_RESPONSE); // read by typed key
String email = context.getContext("storedEmail"); // read by string key
boolean has = context.hasContext(CoreContextKey.RESPONSE); // presence checkYou can store by typed key or by string key — prefer typed keys (§5): they're type-checked on write and can't typo-collide like strings.
Storing the same key again overwrites the previous value — there's no error, the new value simply replaces the old. That's exactly the FluentResponse pattern: a step stores the response, the next steps use it, and then the next call stores a fresh FluentResponse under the same key — replacing the old one — so CoreContextKey.FLUENT_RESPONSE always holds the most recent call.
What you typically store: the FluentResponse from a call, extracted response-body DTOs, tokens, and ids — anything a later step needs. For example, a given a user is logged in step stores the login FluentResponse; a following when the profile is fetched step reads the token out of it.
A typed key names a slot and fixes the Java type of its value (checked on write). There are two enums:
CoreContextKey— framework-level keys (FLUENT_RESPONSE,RESPONSE,REQUEST_SPECIFICATION, …).ContextKey— your SUT-specific keys (ID,TOKEN,CREATE_USER_REQ, …).
To add your own, open ContextKey and add a constant with the class its value will hold:
public enum ContextKey implements TypedContextKey {
ID(Integer.class),
TOKEN(String.class),
CREATE_USER_REQ(CreateUserReq.class); // key -> value type
private final Class<?> type;
ContextKey(Class<?> type) { this.type = type; }
@Override public Class<?> getType() { return type; }
}You can create your own key enums, but they must implement TypedContextKey — an enum with a single getType() returning the value's class. There are no other methods; the interface exists only so the store can enforce, on write, that the value matches the key's declared type.
ScenarioContext is one of two stores. The other is GlobalState:
ScenarioContext |
GlobalState |
|
|---|---|---|
| Scope | one scenario | the whole test run — all scenarios, even across features |
| Lifetime | cleared between scenarios | until explicitly cleared (e.g. suite teardown) |
| Obtained via | injected by PicoContainer (constructor) | static methods (GlobalState.setState/getState) |
| Use for | passing data between steps of one scenario | shared, long-lived data reused by many scenarios |
- ScenarioContext — step A does the login and stores its
FluentResponse; step B reads that response to perform the next operation. Data lives and dies with the scenario. - GlobalState — a one-time precondition logs in all your users and stores their tokens under a key; every later test (any feature) reads them back. Because it's global, it survives across scenarios.
Both share the same key model (typed keys, string keys, and the actor-scoped map below).
⚠️ Be careful what you put inGlobalState. It's for a few specific, long-lived values — a token, a shared id — not a general dumping ground. Two traps to avoid:
- Don't invalidate what you cached. If you store a login token globally, make sure a later step/test doesn't log the same user in again and invalidate the token you stored.
- Never store per-request data (like a response) globally unless you are absolutely certain that state will not change for the rest of the run. Responses and other short-lived values belong in
ScenarioContext, notGlobalState.
Under the hood each store holds three maps. Picture them as JSON:
Flat, by typed key — the common case:
{ "FLUENT_RESPONSE": <FluentResponse>, "ID": 42, "TOKEN": "eyJhbG..." }context.setContext(ContextKey.TOKEN, "eyJhbG..");
String token = context.getContext(ContextKey.TOKEN);Flat, by string key — for ad-hoc names:
{ "storedEmail": "a@b.com", "storedUserId": "42" }Actor-scoped (2-level / "2D") — when several actors each need their own values. The outer key is an actor id (ownerLocalId), the inner is a typed key:
{
"merchant1": { "TOKEN": "eyJ...m1", "ID": 1 },
"admin2": { "TOKEN": "eyJ...a2", "ID": 7 }
}context.setContext("merchant1", ContextKey.TOKEN, tokenM1); // owner + key + value
String m1 = context.getContext("merchant1", ContextKey.TOKEN);Use the flat maps for single-actor scenarios; use the actor-scoped map when a scenario juggles multiple users/services and each needs its own token, id, etc.
A step definition maps a step phrase to a method via @Given / @When / @Then / @And / @But (they're interchangeable at runtime — pick the one that reads best). The method body calls a client/flow and — importantly — stores both the FluentResponse and the raw Response so later steps can act on them:
@When("a new {string} user is created")
public void a_new_user_is_created(String role) {
CreateUserReq req = CreateUserReq.Mother.userWithRole(role);
FluentResponse<UserRes> res = UserCrudClient.createUser(req); // call the client
context.setContext(CoreContextKey.FLUENT_RESPONSE, res); // enables "the status code is {word}"
context.setContext(CoreContextKey.RESPONSE, res.getResponse()); // enables every response-based step
}A feature file then reads in plain English: When a new "customer" user is created.
⚠️ In every step that makes a call, store bothCoreContextKey.FLUENT_RESPONSEandCoreContextKey.RESPONSE(as above). Each unlocks a different set of ready-made generic steps, so you rarely have to write assertion steps yourself.
FLUENT_RESPONSEenables the semantic status step fromGenericStatusAssertionStepDef:Then the status code is {word}(thethe_status_code_ismethod) reads the storedFluentResponseand accepts a semantic keyword —SUCCESSFUL,SUCCESS, orPOSITIVE(asserts the client's own success code viaassertStatusCodeSuccuss()) — or a numeric code like404(asserted exactly).RESPONSE(the raw response) enables every generic step that works off the response — exactThen status code is 400, response-body assertions, response-header assertions, and so on. These are all documented in the zero setup usage manual (the assertion sections).
So a wrapper step only has to make the call and store both objects — the status, body, and header assertions are already written:
When a new "customer" user is created
Then the status code is SUCCESSFUL # from FLUENT_RESPONSE
And response body matches: # from RESPONSE — see the zero setup manual
"""
{ "email": "${any}", "role": "customer", ... }
"""Custom parameter types are optional — the built-in {string}, {int}, {word} cover most steps. Define your own only when you want a step argument to arrive as a specific type (e.g. an enum, like this project does for {httpMethod}) or you want tidier, validated syntax.
Placeholders like {string}, {int}, {word} in a step phrase are parameter types. You define your own in ParameterTypes with @ParameterType — give it a regex and a method that converts the matched text into any Java type:
@ParameterType("true|false|yes|no|enabled|disabled|1|0") // regex that matches the token
public boolean bool(String value) { // converts it to a boolean
return switch (value.toLowerCase()) {
case "true", "yes", "enabled", "1" -> true;
default -> false;
};
}Used in a step phrase as {bool}:
@Given("admin updates allow-buy to {bool}")
public void admin_updates_allow_buy(boolean allowed) { ... }The regex can be anything you want (a fixed set of words, [A-Z_]+ for an enum name, a number pattern, …), and the method can return an enum, a DTO, or any type — so your steps read naturally and arrive already typed. This project uses this for {bool}, {httpMethod}, {storageScope}, and others.
- Do the script-style setup first (base URL, DTOs, clients, optional flows) — see the custom script guide.
- Wrap each call in a Cucumber step — a step-def method under
src/test/java/api/tests. Examples:UserCrudStepDef,UserProfileStepDef. - Share data via injected
ScenarioContext(Cucumber PicoContainer); prefer typed keys (ContextKey/CoreContextKey); add your own key by implementingTypedContextKey. - Scope matters:
ScenarioContext(one scenario) vsGlobalState(whole run). Store theFluentResponse, DTOs, tokens, ids; use the actor-scoped map for multi-actor scenarios. - Add custom step parameters with
@ParameterTypeinParameterTypes.
Most valuable for big, mixed teams where non-developers help author tests. It's the most work to build and keep in sync — that's the price of the readability.