String transformations using @Format in Cucumber

Note: This annotation no longer supported with new Cucumber Version.

Scaler type is a type that can be derived from a single String value. Cucumber JVM allows converting Strings into various Scaler types. You can convert your Strings in test Steps to some specific String in your code. 

Cucumber JVM's build in Scaler types are numbers, enums, and java.util.calender etc.

Annotation Type Format

@Format annotation can be specified on step definition method parameters to give Cucumber a hint about how to transform a String into an object such as a Date or a Calendar.  For example, if you have the following Gherkin step:

#String transformation example
  Scenario: Enter date in different format
  Given The date is 2012-03-01T06:54:12
  And The calender date is 2012-03-01T06:54:12

Then the step definition will be

@Given("^The date is (\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})$")
 public void the_date_is(@Format("yyyy-MM-dd'T'HH:mm:ss") Date date) 
  Date expectedDate = date;
  System.out.println("Expected Date:"+expectedDate);
@Given("^The calender date is (\\d{4}-\\d{2}-\\d{2}T\\d{2}:\\d{2}:\\d{2})$")
 public void the_date_is(@Format("yyyy-MM-dd'T'HH:mm:ss") Calendar cal) 
   Calendar expectedDate = cal;
   System.out.println("Expected Date:"+expectedDate);
Tue, 03/02/2021 - 17:56
Tarun Goswami is a highly experienced professional, currently serving as Technical Lead at a well-established IT services company. With over 12 years of experience across multiple domains, he demonstrates his expertise and passion for the industry through his engaging and informative blog writing in his spare time.