liveBook · Manning

Parameter type annotation

As explained in BDD in Action, Second Edition

The ParameterType annotation is a Cucumber annotation that identifies a method defining a custom parameter type.

The ParameterType annotation identifies a method that converts text from a Cucumber Expression into a domain value for a step definition. The method declares a regular expression for matching the text and returns the converted object. By default, the parameter type name comes from the method name; use the name attribute when the desired name does not follow Java method naming conventions. section 8.4.3

Place parameter type methods in step definition classes or elsewhere in the glue code packages. Reusable domain-specific types make step definitions shorter and let scenarios express meaningful concepts instead of exposing conversion details. section 8.4.3

Converting a frequent-flyer member

A custom type can recognize statuses such as “Standard Frequent Flyer member” or “Silver Frequent Flyer member,” convert the status, and construct a FrequentFlyerMember. A step definition can then use {frequentFlyer} directly alongside ordinary parameters such as {int}.

Defining an ISO date

A named parameter type can match YYYY-MM-DD text and return a LocalDate. The name attribute exposes it as ISO-date, allowing a step such as “{} joined the Frequent Flyer programme on {ISO-date}”.

Listing 8.7 A parameter type definition
@ParameterType("(Standard|Silver|Bronze|Gold) Frequent Flyer member")      #1
public FrequentFlyerMember frequentFlyer(String statusName) {            
    FrequentFlyerStatus status = FrequentFlyerStatus.valueOf(statusName);  #2
    return FrequentFlyerMember.withStatus(status);                         #3
}

Inspect the complete custom parameter definition: its matching expression, status conversion, and domain-object construction. section 8.4.3

How BDD in Action, Second Edition covers it

How deep it goes
Explains matching expressions, naming, object conversion, placement, and reuse, but does not detail annotation runtime internals.
Examples
Demonstrates frequent-flyer members, ISO dates, and comma-separated destination lists converted into typed values.
What it assumes
Assumes Cucumber step definitions and expressions, while demonstrating regular-expression matching and domain-value conversion.
What you can do after
Build it from scratch by defining matching expressions, naming custom types, converting text, and applying them in step definitions.
How it connects
Connects Cucumber Expressions, step definitions, regular expressions, domain objects, and data conversion.

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.