String transformations using @Format in Cucumber

Profile picture for user devraj

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, java.util.date 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
  @smoke
  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);
 }