1

In JBehave, BeforeStory or AfterStory annotation can be added in Step level. So if there are multiple Steps classes with BeforeStory annotation, all those BeforeStory annotated methods will be executed before the each and every story start (which is not needed).

In JUnit we can add or leave BeforeClass or Before as needed in test classes separately. So what I need is a way to add BeforeStory or AfterStory in a story level just like JUnit test class.

Is there a way to add BeforeStory as a lifecycle in a story file ? or any alternative solution ?

Following example shows only adding Before in the lifecycle.

http://jbehave.org/reference/stable/story-syntax.html

Lifecycle: 
Before:
Given a step that is executed before each scenario 
After:
Outcome: ANY    
Given a step that is executed after each scenario regardless of outcome
Outcome: SUCCESS 
Given a step that is executed after each successful scenario
Outcome: FAILURE 
Given a step that is executed after each failed scenario

Thanks.

2 Answers2

0

In my opinion jbehave does not have such a facility as of now. But try the following - in the class file execute a Given to initialize users and in the end of the story scenarios define another Given to release resources. I know this approach breaks the BDD paradigm of whole and business defined scenarios, but atleast is a work around. To ensure it executes first and last you could use (priority=1) for initUsers() and (priority=n) for cleanup().

0

As you have quoted from the documentation, Before and After steps are executed after each scenario and not step as was stated. There is no facility to have a 'before story' step defined in a story file.

An alternative to define something in a story file is to annotate method with @BeforeStory as described in: http://jbehave.org/reference/stable/annotations.html

cooltea
  • 1,113
  • 7
  • 16