@BeforeScenario and @AfterScenario
Often it is useful to be able to set up the scenario environment before running it and and tearing it down after having run it.
JBehave allows scenario developers to do so using annotations: BeforeScenario and AfterScenario.
Let’s walk through an example, e.g. the noughtsandcrosses one. First, let’s define our Steps instance that will be responsible for managing the before and after scenario behaviour:
public class BeforeAndAfterSteps extends Steps { private final OAndXUniverse universe; public BeforeAndAfterSteps(OAndXUniverse universe) { this.universe = universe; } @BeforeScenario public void runThisBeforeScenarios() throws Exception { universe.reset(); } @AfterScenario public void runThisAfterScenarios() throws Exception { universe.destroy(); } }
Then, let’s added it the Steps being passed to the Scenario:
public PlayersCanHazTurns(final ClassLoader classLoader, OAndXUniverse universe) { super(new MostUsefulConfiguration() { public KeyWords keywords() { return new KeyWords("I can haz", "Gief", "Wen", "Den", "And"); } public ClasspathScenarioDefiner forDefiningScenarios() { return new ClasspathScenarioDefiner(new UnderscoredCamelCaseResolver(), new PatternScenarioParser(this), classLoader); } }, new LolCatzSteps(universe), new BeforeAndAfterSteps(universe)); }
Finally, just run scenario and the methods annotated with @BeforeScenario and @AfterScenario will be performed before and after each scenario.
NOTE: the before/after steps are not required to be in a separate Steps instance, but we recommend this separation so it can be reused by several scenarios.
AfterScenario Outcome
The @AfterScenario annotation has an optional Outcome parameter that allows the developer to declare if the method should be performed, depending on the Outcome. By default it is performed upon any outcome, but it can be executed only upon success or failure:
public class BeforeAndAfterSteps extends Steps { private final OAndXUniverse universe; public BeforeAndAfterSteps(OAndXUniverse universe) { this.universe = universe; } @BeforeScenario public void runThisBeforeScenarios() throws Exception { universe.reset(); } @AfterScenario(uponOutcome=AfterScenario.Outcome.SUCCESS) public void runThisAfterSuccessfulScenarios() throws Exception { universe.destroy(); } @AfterScenario(uponOutcome=AfterScenario.Outcome.FAILURE) public void runThisAfterFailedScenarios() throws Exception { universe.destroy(); // may want to do some other cleanup action here } }
