liveBook · Manning

Step definition

As explained in BDD in Action, Second Edition

A step definition is an executable mapping that connects a human-readable behavior-specification step to code that performs or verifies the corresponding application interaction.

A Gherkin scenario can say that a traveler completes a flight and earns 550 points, but Cucumber cannot work out how to record the flight or check the points from those words alone. A step definition supplies the missing link: it matches a scenario step to executable code that performs an action or verifies an outcome. section 8.1 section 8.1.1

Turning a Given step into setup

For “Given Tara is a Standard Frequent Flyer member,” a Java @Given method can create a member named Tara with Standard status. The scenario states the business condition; the method establishes it for the test. section 8.4

Hard-coding every name or status creates duplicate definitions. A Cucumber Expression can match variable parts of a step and pass them as method parameters. For example, {word} can capture a name and a membership status, allowing one definition to serve related scenarios. Other expression types handle values such as integers and quoted strings. section 8.4.1

Listing 8.5 A step definition method with a parameter (in Java)
FrequentFlyerMember member;                        
 
@Given("{word} is a {word} Frequent Flyer member")                           #1
public void aFrequentFlyerMember(String name, String status) {               #2
    FrequentFlyerStatus statusLevel = FrequentFlyerCategory.valueOf(status); #3
    member = FrequentFlyerMember.named(name).withStatus(statusLevel);
}

The Java listing shows the complete link: placeholders in the @Given pattern become arguments to a method that creates the member. section 8.4.1

Keep the step definition focused on translating business language into tasks and actions. Reusable tasks can sit beneath it, while a separate system-interactions layer handles details such as locating web elements or calling REST endpoints. A Screenplay-style definition, for instance, can map a sign-up step to tasks for signing up and verifying submission. section 9.3.1 section 14.3

Matching matters: if scenario wording changes without a corresponding definition, the step becomes undefined or pending rather than silently running the wrong code. Cucumber can suggest glue-code snippets for missing steps, and IDE integrations can help navigate to definitions and flag unmatched steps. Automate scenarios where doing so is worthwhile; the shared understanding captured in the scenario remains valuable even when automation is impractical. section 8.1.1 section 8.4.1 section 8.0

How BDD in Action, Second Edition covers it

How deep it goes
Works through matching methods and parameter extraction, then separates step glue from reusable tasks and system interactions.
Examples
Uses frequent-flyer membership and flight scenarios, plus a sign-up step mapped to Screenplay tasks.
What it assumes
Assumes familiarity with Given–When–Then scenarios and teaches how their steps become executable methods.
What you can do after
Build it from scratch: match scenario wording, pass captured values into code, and delegate actions to reusable tasks.
How it connects
Links Gherkin, Cucumber Expressions, acceptance tests, and the Screenplay Pattern.

Also explained in

Compare how 2 books explain step definition →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of BDD in Action, Second Edition. The section links point to the parts of the book it draws on.