7

I have a cucumber stepdef like this

Given the date of <date>
When blah blah
Then x y and z
Examples:
|2015-01-01|
|2045-01-01|

When I generate stepdefs off of this, I get @Given("^the date of (\\d+)-(\\d+)-(\\d+)$") And the method is generated with three integers as parameters. How can I tell Cucumber to treat it like a Java.Time LocalDate? Is there a way to create a mapper that Cucumber will understand? Or at the very least, is there a way to treat that date object as a string instead of three numbers?

Steve
  • 4,457
  • 12
  • 48
  • 89

3 Answers3

9

Modify your step definition to take in a String for the whole date. Maybe use something like (.*?) instead of 3 integers.

@Given("^the date of (.*?)$")
public void storeDate(@Transform(DateMapper.class) LocalDate date){

}

Transformer class

public class DateMapper extends Transformer<LocalDate>{

    @Override
    public LocalDate transform(String date) {

        //Not too sure about the date pattern though, check it out if it gives correct result
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");

        return LocalDate.parse(date, formatter); 
   }

}

Cucumber should transform the string format into a date object for you

Grasshopper
  • 8,908
  • 2
  • 17
  • 32
  • i am still getting @Given("^the date of (\\d+)-(\\d+)-(\\d+)$") And the method is generated with three integers as parameters. How can I tell Cucumber to trea error – d-man Dec 09 '16 at 20:44
  • You are using the default method stepdef pattern and arguments generated by cucumber. Edit it to the string pattern and argument you want to get data into the method. – Grasshopper Dec 10 '16 at 05:15
5

With Cucumber 7, I'm defining a new @ParameterType :

@ParameterType("\\d{2}\\.\\d{2}\\.\\d{4}")
public LocalDate mydate(String dateString) {
    return LocalDate.parse(dateString, DateTimeFormatter.ofPattern("dd.MM.yyyy"));
}

Then I can use step definition @Given such as :

@Given("person has birthdate {mydate}")
public void person_birthdate(LocalDate birthDate) {
    ... // do something
}

The placeholder name {mydate} is the name of the mapping method, but you can override it through @ParameterType.name.

Julien Kronegg
  • 4,968
  • 1
  • 47
  • 60
2

Please check if this works -

Scenario Outline: Date  
Given the date of <date>
When blah blah
Then x y and z
Examples:
|date      |
|2015-01-01|
|2045-01-01|

I have modified the default step definition like this -

@Given("^the date of (.*?)$")
public void the_date_of(String strDate) throws Throwable {
    SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.ENGLISH);
    Date date = format.parse(strDate);
    System.out.println(date);
}

This prints the code like this -

Thu Jan 01 00:00:00 AEDT 2015
Sun Jan 01 00:00:00 AEDT 2045
Anish Pillai
  • 1,023
  • 7
  • 8
  • still gets @Given("^the date of (\\d+)-(\\d+)-(\\d+)$") And the method is generated with three integers as parameters. How can I tell Cucumber to trea error – d-man Dec 09 '16 at 20:44