mdzh
mdzh

Reputation: 1050

jBehave is it possible to capture pattern with multiple choices?

I have the following two almost-identical Steps that verify if a property is either null or not:

@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(...)
}

I feel like there must be some better way to handle this null vs non-null use case instead of duplicating the Steps. Is there a way to capture the pattern as in

@Then("groupId of $allocationName {is|is not} null")
public void thenGroupIdOfAllocationIsNull(String allocationName, boolean isNot) {
    // Some logic here
    ...
    isNot ? assertNotNull(...) : assertNull(...);
}

How do I achieve that with jBehave?

Upvotes: 1

Views: 43

Answers (1)

VaL
VaL

Reputation: 1167

Enum could be used to provide options:

@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(...);
}

Upvotes: 1

Related Questions