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
