## Lifecycle

JBehave supports two main ways to control the lifecyle of a story:

- Programmatically via annotations.
- Declaratively via the Lifecycle element of the story syntax.

### Programmatic Lifecycle

The programmatic lifecycle controls are code-centric and are best suited for technical - rather than functional - controls.

The @BeforeScenario and @AfterScenario annotations allow the corresponding methods to be executed before and after each scenario. Optionally, the ScenarioType can be specified to execute only upon that type, e.g. for scenarios parametrised by examples. In addition, @AfterScenario allows the setting of an optional Outcome value, which specifies whether the method should be executed depending on the outcome of the scenario.

Similarly, the @BeforeStory and @AfterStory annotations allow the corresponding methods to be executed before and after each story, either a GivenStory or not:

Finally, the @BeforeStories and @AfterStories annotations allow the corresponding methods to be executed before and after a collection of stories:

### Declarative Lifecycle

The declarative lifecycle controls are story-centric and best suited for describing functional behaviour that needs to be repeated before and after scenarios and stories.

The declarative controls mirror the annotations-based ones.

To define steos that need to be executed before or after each scenario:

    Lifecycle:
    Before:
    Scope: SCENARIO
    [steps to be executed before each scenario]
    After:
    Scope: SCENARIO
    [steps to be executed after each scenario]

Similarly, to define steos that need to be executed before or after each story:

    Lifecycle:
    Before:
    Scope: STORY
    [steps to be executed before each story]
    After:
    Scope: STORY
    [steps to be executed after each story]

Scopes can also be mixed and matched:

Similarly, to define steos that need to be executed before or after each story:

    Lifecycle:
    Before:
    Scope: STORY
    [steps to be executed before each story]
    Scope: SCENARIO
    [steps to be executed before each scenario]
    After:
    Scope: SCENARIO
    [steps to be executed after each scenario]
    Scope: STORY
    [steps to be executed after each story]

After steps also allow for outcome-based controls.

    Lifecycle:
    After:
    Scope: SCENARIO|STORY
    Outcome: ANY
    [steps to executed after any scenario or story]
    Outcome: SUCCESS
    [steps to executed after a successful scenario or story]
    Outcome: FAILURE
    [steps to executed after a failed scenario or story]

Lifecycle scopes and outcomes are keywords that can be specified in any configured locale.