A readable scenario does not run itself. In Cucumber, glue code connects each Given, When, and Then line to a step definition method. Cucumber matches the line, passes in its test data, and runs the method’s automation code. section 8.4 section 3.5.3
Finding the next train
A scenario supplies scheduled departures, requests a trip, and expects particular train times. Its Given method prepares the timetable; its When method asks an itinerary service for departures; its Then method compares the result with the expected times. Annotations and Cucumber Expressions bind the scenario wording and data to those methods. section 3.5.3 section 3.5.4
Listing 3.2 A basic Cucumber scenario implementation
package com.bddinaction.traintimetables.stepdefinitions;
import io.cucumber.java.ParameterType;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class DepartingTrainsStepDefinitions {
@Given("the {} train to {} leaves {} at {}")
public void theTrainLeavesAt(String line,
String to,
String from,
String departureTimes) {}
@When("Travis want to travel from {} to {} at {}")
public void travel(String from, String to, String departureTime) {}
@Then("he should be told about the trains at: {}")
public void shouldBeToldAboutTheTrainsAt(String expectedTimes) {}
}
The three annotated methods show where scenario steps attach to code. Their empty bodies make the boundary clear: matching a step is only the start of automating it. section 3.5.3
Starting with Then can clarify the expected result before the service API exists. When can then expose the query the service needs, while Given reveals how to supply its test data. This lets the acceptance test help shape domain classes and service boundaries; lower-level unit tests can drive the underlying behavior. section 3.5.4
Keep scenario wording focused on business outcomes. Step definitions translate those steps into reusable business tasks and actions; a separate system-interactions layer handles details such as web elements or REST endpoints. In a Java Cucumber project, step definitions can live in a stepdefinitions package, apart from feature files and other test automation code. section 9.3.1 section 8.2.2

