Given When Then
I have a love-hate relationship with Gherkin, the syntax underpinning Cucumber tests. On the one hand I love the readability, on the other I find the binding of the sentences to step definitions flaky, and the lack of formal structure in those sentences worries me. Like the Maven vs Gradle debate, I’m a fan of structure; it keeps stuff from derailing quickly. (Maven FTW.)
So when I decided to give Cucumber another go for one of my hobby projects, I attempted to get some structure in (more on that later on). But the result was that I wrote this:
public void test() {
Scenario.of("Modify Vacation Hours")
.given(RosterPeriod.on("2022-09-19").exists())
.and(User.of("peter").isLoggedin())
.when(Overview.isAccessed())
.and(VacationHours.forUser("peter").onDate("2022-09-19").isSetTo(20))
.then(VacationHours.forUser("peter").onDate("2022-09-19").shouldBe(20))
.and(WeekTotals.forUser("peter").inRosterPeriod("2022-09-19").shouldBe(20,0,0,0,0,0))
.and(RunningWeekTotals.forUser("peter").inRosterPeriod("2022-09-19").shouldBe(20,0,0,0,0,0))
.and(Event.who("peter").what("SetVacationHours").user("peter").rosterPeriod("2022-09-19").detailSubstring("hours=20").shouldExist());
}
Which is the Java equivalent of this Cucumber test:
Scenario: Modify Vacation Hours
Given a rosterperiod 2022-09-19 exists
And user peter is logged in
When the overview is accessed
And the vacation hours for peter on 2022-09-19 is set to 20
Then the vacation hours for peter on 2022-09-19 should be 20
And the week totals for peter in rosterperiod 2022-09-19 should be 20,0,0,0,0,0
And the running week totals for peter in rosterperiod 2022-09-19 should be 20,20,20,20,20,20
And an event with who "peter", what "SetVacationHours", user "peter", rosterdate 2022-09-19 and detail substring "hours=20" should exist
And I started wondering if the Java version is readable enough. Because it damn well fixes all my issues I have with Cucumber.
What do you think?
(more…)