The conditional steps mechanism allows the user to choose a step to execute based on the result of predicate evaluation. This mechanism can be used instead of conditional statements like if-else/switch in case of grow of number of steps with the same naming but different internal logic.
Let's consider the following switch operator with a bunch of conditions:
@Given("food for my pet")
public void prepareFoodForMyPet() {
String partOfDay = ...;
switch (partOfDay) {
case "morning":
// logic for morning
break;
case "evening":
// logic for evening
break;
case "afternoon":
// logic for evening
break;
}
}
With conditional steps the logic can be rewritten in the following way:
@Conditional(condition = PartOfDayCondition.class, value = "morning")
@Given("food for my pet")
public void prepareMorningFood() {
// logic for morning
}
@Conditional(condition = PartOfDayCondition.class, value = "evening")
@Given("food for my pet")
public void prepareEveningFood() {
// logic for evening
}
@Conditional(condition = PartOfDayCondition.class, value = "afternoon")
@Given("food for my pet")
public void prepareAfternoonFood() {
// logic for afternoon
}
To make this work we need the following condition implementation:
public class PartOfDayCondition implements Predicate\
By default conditions are checked by DefaultStepConditionMatcher that requires conditions to have public non-args constructor, custom step condition matcher can be created by implementing the StepConditionMatcher interface and then registering the new implementation in a Configuration instance.
Notes: