liveBook · Manning

Glue code

As explained in BDD in Action, Second Edition

Glue code is integration code that connects executable behavior specifications or test steps to application code and services.

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

How BDD in Action, Second Edition covers it

How deep it goes
Works through annotated methods, test-data conversion, and service design; develops underlying behavior through separate unit tests.
Examples
Illustrates train-departure step methods and frequent-flyer setup in Java and TypeScript.
What it assumes
Uses Given/When/Then scenarios and Java or TypeScript snippets; defines step definitions before relying on an existing service API.
What you can do after
Build it from scratch by binding steps to methods, converting test data, calling services, and checking results.
How it connects
Links Gherkin, Cucumber Expressions, step definitions, automated acceptance tests, and test-driven development.

Also explained in

Compare how 2 books explain glue code →

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.