Skip to content

Instantly share code, notes, and snippets.

@rkie
Created January 27, 2018 16:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rkie/e1861e4ee64030a34950296308a123cf to your computer and use it in GitHub Desktop.
Save rkie/e1861e4ee64030a34950296308a123cf to your computer and use it in GitHub Desktop.
Cucumber date format workaround
Feature: Demonstration of some date workarounds
Scenario: Simple formatting of dates with annotation
Given the dates 2018-01-01 and 2018-01-31
When the earliest date calc is run
Then the earliest date is 2018-01-01
Scenario: Failing scenario: date format is wrong
Given the list of dates: 2018-01-01,2018-01-31
Scenario: Earliest date as list with US month first format, uch!
Given the list of dates: 1/1/18, 1/31/18
When the earliest date calc is run
Then the earliest date is 2018-01-01
Scenario: Earliest date as table with comma in format
Given the table of dates:
| Jan 1, 2018 |
| May 2, 2018 |
When the earliest date calc is run
Then the earliest date is 2018-01-01
Scenario: Failing scenario: Who is youngest - wrong date format
Given the following people
| firstName | lastName | dateOfBirth |
| John | Doe | 1990-05-31 |
| Jane | Doe | 1995-01-01 |
When the youngest calc runs
Then the answer is Jane Doe
Scenario: Birthdays - with birth dates
Given the following people
| firstName | lastName | dateOfBirth |
| John | Doe | May 31, 1990 |
| Jane | Doe | Jan 1, 1995 |
When the youngest calc runs
Then the answer is Jane Doe
And the first person in the list should still be John
package com.failedtofunction.example.cumcumberdates;
import static org.junit.Assert.assertEquals;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import com.failedtofunction.example.cucumberdates.DateUtilities;
import com.failedtofunction.example.cucumberdates.Person;
import cucumber.api.Format;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class Steps {
private Date earliestDate;
private List<Date> dates;
private String youngestResult = "the youngest";
private List<Person> people;
@Given("^the dates (.*) and (.*)$")
public void givenDates(
@Format("yyyy-MM-dd") Date first,
@Format("yyyy-MM-dd") Date second) {
this.dates = new ArrayList<>();
dates.add(first);
dates.add(second);
}
@When("^the earliest date calc is run$")
public void earliestDate() {
earliestDate = DateUtilities.earliestDate(dates);
}
@Then("^the earliest date is (.*)$")
public void checkEarliestDate(@Format("yyyy-MM-dd") Date expected) {
assertEquals("Wrong earliest date", expected, earliestDate);
}
@Given("^the list of dates: (.*)$")
public void dateList(List<Date> dates) {
this.dates = dates;
}
@Given("^the table of dates:$")
public void dateTable(List<Date> dates) {
this.dates = dates;
}
@Given("^the following people$")
public void givenPeople(List<Person> people) {
this.people = people;
}
@When("^the youngest calc runs$")
public void youngestCalc() throws Exception {
Person youngest = DateUtilities.youngest(people);
youngestResult = String.format("%s %s", youngest.getFirstName(), youngest.getLastName());
System.out.println("The youngest is: " + youngestResult);
}
@Then("^the answer is (.*)$")
public void youngestIs(String youngest) throws Exception {
assertEquals(youngest, youngestResult);
}
@Then("^the first person in the list should still be (.*)$")
public void firstPersonInListStill(String expected) throws Exception {
assertEquals(expected, people.get(0).getFirstName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment