我有以下两个几乎相同的步骤来验证属性是否为null:
@Then("groupId of $allocationName is not null")
public void thenGroupIdOfAllocationIsNotNull(String allocationName) {
// Some logic here
...
assertNotNull(...)
}
@Then("groupId of $allocationName is null")
public void thenGroupIdOfAllocationIsNull(String allocationName) {
// Some logic here
...
assertNull(...)
}我觉得必须有更好的方法来处理这个空用例和非空用例,而不是重复步骤。是否有一种方法可以捕获模式,如
@Then("groupId of $allocationName {is|is not} null")
public void thenGroupIdOfAllocationIsNull(String allocationName, boolean isNot) {
// Some logic here
...
isNot ? assertNotNull(...) : assertNull(...);
}如何使用jBehave实现这一目标?
发布于 2022-11-24 07:04:02
Enum可用于提供备选方案:
@Then("groupId of '$allocationName' $nullEqualityCheck null")
public void checkGroupIdOfAllocation(String allocationName, NullEqualityCheck nullEqualityCheck) {
// Some logic here
...
nullEqualityCheck.check(...);
}public enum NullEqualityCheck {
IS {
public void check(...) {
assertNull(...)
}
},
IS_NOT {
public void check(...) {
assertNotNull(...)
}
};
public abstract void check(...);
}https://stackoverflow.com/questions/74547324
复制相似问题