0

I want to write a "common" story for my acceptance tests in JBehave that adds an user only if he does not exists. The "createUser" story is an acceptance test case itself. I want to refer to it with GivenStories e.g. on "modifyUser", "deleteUser" etc - but I don't want to get an error when "createUser" story wants to add an user that already exists.

How can I ensure that the "createUser" is executed only when it is needed? I can't find any solution for "conditional stories" in JBehave. I can't write the "createUser" story as idempotent too.

fracz
  • 20,536
  • 18
  • 103
  • 149

1 Answers1

1

This is not an issue for JBehave itself - it will not be handled on this level. I guess you want a scenario like this:

Given user "John" exists with ID 1234
When delete user request for ID 1234 was processed
Then no user entry for ID 1234 should exist

Only within the binding code to the given step you'd check and create if necessary the user (perform your conditional insert operation). This is handled within your Java code, not on the acceptance test level. It would look something like this:

@Given("Given user \"$userName\" exists with ID $userId")
public void givenUserExistsWithId(String userName, int userId)
{
    if (!persistence.existsUser(userId))
    {
        persistence.createUser(userId, userName);
    }
}

That way the scenario is clean and the code ensures the expected state after the given state was executed.

dertseha
  • 1,086
  • 8
  • 11