← Back to libraryQuestion 251 of 468
🥒Cucumber / BDDBeginner

Step Definitions and Glue Code

📌 Definition:

Step definitions are the code (methods) that implement Gherkin steps. Each is annotated with @Given/@When/@Then and a pattern that matches the step text; this mapping layer is called the 'glue'.

📖 Detailed Explanation:

When Cucumber reads a step like 'When the user enters valid credentials', it finds the step-definition method whose pattern matches that text and runs it. In Java: @When("the user enters valid credentials") public void enterCreds() { loginPage.login(user, pass); }. Parameters in the step (numbers, quoted strings) are captured and passed as method arguments. The 'glue' path tells Cucumber where the step-definition classes live. One step definition can serve many scenarios, so reusable, parameterized steps reduce duplication. A missing/ambiguous match causes an undefined or ambiguous step error.

🔑 Key Points:
  • @Given/@When/@Then map step text → methods (the glue)
  • Captured parameters (numbers, quoted strings) become method args
  • One reusable step definition can serve many scenarios
  • Glue path tells Cucumber where step classes live
🌍 Real-World Example:

@Then("the order total should be {double}") public void checkTotal(double expected) { assertEquals(expected, cart.total()); } — one parameterized step verifies any expected total across many scenarios.

🎯 Scenario-Based Interview Question:

A scenario fails with 'Undefined step'. What does it mean and how do you fix it?