</>

Technology

Cucumber

Difficulty

Beginner

Interview Question

What is the use of the glue property under @CucumberOptions?

Answer

glue Property in @CucumberOptions

Purpose

The glue property tells Cucumber where to find the step definitions and hooks. It specifies the Java package(s) that contain classes annotated with @Given, @When, @Then, @Before, and @After.

Glue is the binding between plain English Gherkin steps and actual Java implementation code.

Syntax

Java
@CucumberOptions(
    glue = "stepDefinitions"
    // OR
    glue = {"stepDefinitions", "hooks"}
)

Single Package

Java
@CucumberOptions(
    features = "src/test/resources/features",
    glue = "stepDefinitions"   // All step defs in stepDefinitions package
)

Multiple Packages

Java
@CucumberOptions(
    features = "src/test/resources/features",
    glue = {
        "stepDefinitions",    // Step definition classes
        "hooks",              // @Before/@After hook classes
        "utils"               // Utility classes with @ParameterType
    }
)

Project Structure with Glue

CODE
src/
  test/
    java/
      stepDefinitions/        ← glue = "stepDefinitions"
        LoginSteps.java
        RegistrationSteps.java
        CheckoutSteps.java
      hooks/                  ← glue includes "hooks"
        Hooks.java
        ScreenshotHook.java
      runners/
        TestRunner.java

What Happens Without Correct Glue

CODE
Error: io.cucumber.junit.UndefinedStepException
  Step "When the user clicks Login" is undefined

Reason: Cucumber couldn't find the step definition
        because the package isn't listed in glue

Common Mistakes

Java
// WRONG — wrong package name
glue = "stepDefinition"   // should be "stepDefinitions"

// WRONG — using file path instead of package name
glue = "src/test/java/stepDefinitions"  // should be just "stepDefinitions"

// CORRECT
glue = {"stepDefinitions", "hooks"}

Difference: features vs glue

PropertyPurposeExample
featuresWhere .feature files are (file path)"src/test/resources/features"
glueWhere step definitions are (Java package)"stepDefinitions"

Follow AutomateQA

Related Topics