skip to content

Step Definitions & Expressions

How a Gherkin step binds to code: Cucumber Expressions against regex, built-in and custom parameter types, and what the runner reports for an unmatched step. Matching rules reveal real hands-on work.

on this pageshow

explore

questions

5

In a Cucumber Expression, what do {int}, {string}, {word} and {} each capture?

level: juniorimportance: must knowfreq 72%

answer

  1. each brace both matches and converts
  2. quotes are part of the pattern
  3. one token versus quoted text
  4. the empty braces defer to the signature

basics

~20 s

In a Cucumber Expression, {int} captures a whole number and passes an integer, {string} captures quoted text and strips the quotes, {word} captures one token with no whitespace, and {} captures anything and converts it to the method parameter's type.

solid answer

~40 s

Each `{...}` in a **Cucumber Expression** both constrains what the pattern matches and decides the value passed to the step-definition method, positionally. `{int}` matches an optionally signed whole number and arrives as an integer; `{float}` matches a decimal; `{word}` matches a single token with no whitespace and arrives as text; `{string}` matches text in single or double quotes and arrives **without** the quotes; `{}` matches anything and is converted to the method parameter's declared type. Cucumber-JVM adds JVM-specific numeric placeholders such as `{long}`, `{double}` and `{bigdecimal}`. So `the planner logs {int} downtime hours for turbine {string}` matches `the planner logs 37 downtime hours for turbine "T-142"` and calls the method with `37` and `T-142`.

code

java · 4 lines
java
@When("the planner logs {int} downtime hours for turbine {string}")
public void logDowntime(int hours, String turbineId) {
    plan.recordDowntime(turbineId, hours);
}

go deeper

for a junior

Memorise the four everyday placeholders and what each one matches. Be able to read a step line and say which expression would bind to it, and which method arguments the runner would supply.

for a middle

Explain the mechanics: placeholders map positionally onto method parameters, quotes belong to the {string} pattern rather than the value, and the anonymous placeholder defers conversion to the declared parameter type.

for a senior

Show judgement about which placeholder to choose on a shared suite — where a loose one invites ambiguous matches, and where the domain deserves a custom parameter type rather than yet another string argument.

for a principal

Argue for a house style: which placeholders are the default vocabulary, when a domain type replaces a raw string, and how that keeps step signatures readable as a suite grows across teams.

## What a parameter type is doing A **Cucumber Expression** is a step-definition pattern written in near-plain text, with the variable parts marked by a **parameter type** in curly braces. Each `{...}` does two jobs at once: it decides what text the pattern will match at that position, and it decides what value the runner hands to the step-definition method. That second job is what separates the expression syntax from a bare regular expression, where you get the captured text and convert it yourself. Given the Gherkin line: ```gherkin When the planner logs 37 downtime hours for turbine "T-142" ``` the expression `the planner logs {int} downtime hours for turbine {string}` matches it, and the method is called with the number `37` and the string `T-142` — note the quotes are gone. ## The built-in set | Placeholder | Matches | Passed to the method as | |---|---|---| | `{int}` | an optionally signed whole number, e.g. `37`, `-4` | an integer | | `{float}` | a decimal number, e.g. `2.75`, `-0.5` | a floating-point number | | `{word}` | a single token containing no whitespace | the token, as text | | `{string}` | text wrapped in single or double quotes | the text **without** the surrounding quotes | | `{}` | anything at that position | converted to the declared parameter type | Cucumber-JVM adds further numeric placeholders for the JVM's own types — among them `{long}`, `{double}`, `{byte}`, `{short}`, `{bigdecimal}` and `{biginteger}` — because a Java method signature distinguishes them where a JavaScript one does not. Two properties hold across all of them: - **Arguments arrive positionally.** The first placeholder supplies the first method argument, the second the second, and so on. A method whose parameter count does not match the placeholder count is a wiring error, not a runtime mismatch on one scenario. - **Only the placeholder text is variable.** Everything outside the braces is literal text that must be present in the step line, which is why an expression reads like the sentence it matches. ## The anonymous `{}` type `{}` matches any text at that position and leaves the conversion to the method's declared parameter type. If the parameter is a string, the matched text is passed straight through. If it is some other type, Cucumber-JVM needs to know how to build that type from text, which is what a default parameter transformer registered in your glue provides. Used sparingly `{}` is a neat way to keep a step readable; used everywhere it throws away the matching precision that makes expressions useful, because a pattern made mostly of `{}` will happily match sentences you never meant it to. ## Traps worth naming in an interview 1. **`{word}` versus `{string}` for identifiers.** `{word}` stops at whitespace, so a turbine label written as `north ridge 4` needs `{string}` and quotes in the Gherkin, not `{word}`. 2. **The quotes belong to the pattern, not to the value.** With `{string}` the step line must contain the quotes and the method must not expect them; teams that trim quotes inside the step body have usually misread this. 3. **A near-miss is undefined, not failed.** If the line says `4.5` where the expression says `{int}`, nothing matches, so the step is reported as undefined rather than as a type error — the run tells you there is no step definition, which is a confusing message until you know why. 4. **`{int}` is not a validator.** It constrains what the pattern matches; it does not check that the number is sensible. On a wind-farm maintenance planner suite, `-6` downtime hours matches `{int}` perfectly and it is your code's job to reject it. ## Why interviewers ask this The built-in set is the first thing anyone touches on a Cucumber suite, and the answers reveal whether someone has written step definitions or only read them. A candidate who can say that `{string}` strips its quotes, that `{word}` cannot span a space, and that `{}` defers conversion to the method signature has clearly bound real scenarios to real code. The follow-up is almost always about custom parameter types, because that is where the same mechanism starts paying for itself across a 63-scenario feature set that two teams both edit: one place that turns text into a domain object, and step signatures that speak in that domain object rather than in strings.

  • A turbine label can contain a space. Which placeholder do you use, and what changes in the feature file?
    `{word}` stops at whitespace, so it cannot carry `north ridge 4`. Use `{string}` and put quotes around the label in the Gherkin step line; the method still receives the label without the quotes, because the quotes belong to the pattern rather than to the captured value. The alternative is a custom parameter type whose pattern spells out exactly what a label may look like.
  • The step line says 4.5 where the expression says {int}. What does the run report?
    Nothing matches that line, so Cucumber reports the step as undefined and prints a snippet for it — not a type error and not a failure inside the method. `{int}` constrains what the pattern matches, so a decimal simply falls outside it. If decimals are legitimate for that step, the expression needs `{float}` and the method parameter needs to be a floating-point type.
  • When is the anonymous {} placeholder the right choice?
    When the step reads better without a named type and the method's declared parameter type already says what the value is — Cucumber converts the matched text to that type, using a default transformer in your glue for non-string targets. Used for most placeholders it is a liability: a pattern made mostly of `{}` matches sentences you never intended and drags the suite towards ambiguous matches.

saying these in an interview costs you the question

  • Thinks {string} passes the surrounding quotes to the method
  • Believes {word} can match two words separated by a space
  • Says a decimal in the step line makes an {int} step fail rather than go undefined
  • Treats {int} as validation of the business meaning of the number
  • Assumes {} always hands the method a plain string
open as a page

How does Cucumber-JVM decide whether a step definition string is a Cucumber Expression or a regex?

level: middleimportance: must knowfreq 68%

basics

~20 s

Cucumber-JVM reads the annotation string as a regular expression when it starts with a caret, ends with a dollar sign, or is wrapped in forward slashes; otherwise it parses it as a Cucumber Expression. In cucumber-js the argument type decides.

open as a page

In Cucumber, how do undefined, ambiguous and pending step results differ, and which one prints a snippet?

level: middleimportance: should knowfreq 58%

basics

~20 s

Undefined means no step definition matched the line, and Cucumber prints a snippet stub for it. Ambiguous means more than one matched, so the runner refuses to choose. Pending means a matched definition signalled unfinished work. All three stop the scenario.

open as a page

How do you register a custom Cucumber Expression parameter type, and where must that registration live?

level: seniorimportance: should knowfreq 44%

basics

~20 s

Register it in code the runner already loads with your step definitions: in Cucumber-JVM a method annotated @ParameterType, in cucumber-js a defineParameterType call in a support file. The name then becomes a placeholder that converts matched text into a domain object.

open as a page

Why do Cucumber Expressions offer optional text like hour(s) and alternation like logs/records?

level: middleimportance: nice to knowfreq 30%

basics

~20 s

Parentheses make text optional and a slash alternates the words around it, so one Cucumber Expression covers several wordings. The same parentheses in a regular expression would be a capture group demanding another method argument; in an expression they capture nothing.

open as a page