## Writing Textual Stories

Behaviour-Driven Development encourages you to start defining the stories via scenarios that express the desired behaviour in a textual format, e.g.:

        Given a stock of symbol STK1 and a threshold of 10.0
        When the stock is traded at 5.0
        Then the alert status should be OFF

The textual scenario should use the language of the business domain and shield away as much as possible the details of the technical implementation. Also, it should be given a name that is expressive of the functionality that is being verified, i.e. trader_is_alerted_of_status.story.

The scenario should use a syntax compatible with the Grammar.

A story is a collection of scenarios, each detailing different examples of the behaviour of a given increment of functionality of the system.

        Scenario:  trader is not alerted below threshold

        Given a stock of symbol STK1 and a threshold of 10.0
        When the stock is traded at 5.0
        Then the alert status should be OFF

        Scenario:  trader is alerted above threshold

        Given a stock of symbol STK1 and a threshold of 10.0
        When the stock is traded at 11.0
        Then the alert status should be ON

## Mapping Textual Scenario Steps to Java Methods via annotations

JBehave maps textual steps to Java methods via CandidateSteps. The scenario writer need only provide annotated methods that match, by regex patterns, the textual steps.

    public class TraderSteps { // look, Ma, I'm a POJO!!

        private Stock stock;

        @Given("a stock of symbol $symbol and a threshold of $threshold")
        public void aStock(String symbol, double threshold) {
            stock = new Stock(symbol, threshold);
        }

        @When("the stock is traded at $price")
        public void theStockIsTradedAt(double price) {
            stock.tradeAt(price);
        }

        @Then("the alert status should be $status")
        public void theAlertStatusShouldBe(String status) {
            ensureThat(stock.getStatus().name(), equalTo(status));
        }

    }

The scenario writer can specify the annotated methods in a POJO, i.e. without needing to extend/implement any JBehave class/interface. Of course, the Steps instances may inherit or implement other class/interface as required by the model of the application under test.

To create instances of CandidateSteps we use an instance of InjectableStepsFactory

        Configuration configuration = ... // optional configuration
        InjectableStepsFactory factory = new InstanceStepsFactory(configuration, new TraderSteps(), new BeforeAndAfterSteps());

Each step is annotated with one of the step annotations, each holding a regex pattern as value. The pattern is used to match the method in the Steps class with the appropriate parameters. The simplest default behaviour identifies arguments in the candidate step by the words prefixed by the $ character. More advanced parameter injection mechanisms are also supported by JBehave.

JBehave execute all the matched steps in the order in which they are found in the scenario. It is up to the implementor of the Steps classes to provide the logic to tie together the results of the execution of each step. This can be done by keeping state member variables in the Steps class or possibly by using a service API or other dependency.

## Configuring Java Embeddable classes

At the heart of the JBehave running of stories lies the Embedder, which provides an entry point to all of JBehave's functionality that is embeddable into other launchers, such as IDEs or CLIs. JBehave complements the Embedder with an Embeddable which represents a runnable facade to the Embedder.

JBehave allows many different ways to configure Embeddable Java classes that allow the parsing and running of textual stories.

JBehave provides two main Embeddable implementations:

- ConfigurableEmbedder: allows the specification of the Configuration and CandidateSteps.
- InjectableEmbedder: allows the injection of a fully specified Embedder.

### JUnit-enabled Embeddables

JUnit is supported out-of-the-box via several Embeddables implementations:

- JUnitStory: provides a one-to-one mapping with the textual story via the StoryPathResolver.
- JUnitStories: provides a many-to-one mapping with the textual story paths explicitly specified by overriding the storyPaths() method.

In the case of one-to-one mapping, our abstract base TraderStory would look like:

We then extend this base class with a class for each story, e.g. `TraderIsAletedOfStatus.java`, which maps to out textual story in same package.

In the case of many-to-one mapping:

### JUnit AnnotatedEmbedderRunner

JBehave also provides an implementation of JUnit's Runner, AnnotatedEmbedderRunner, which is runnable via JUnit's @RunWith annotation:

### Integration with other frameworks

As remarked above, JBehave does not impose any tie-in with any framework to run stories. It only requires access to the Embedder to run the stories. The following snippet shows, for example, how to use SpringJUnit4ClassRunner to compose and inject steps instances and them the stories:

## What Next?

JBehave provides fully annotatation-based support for specifying configuration and dependency injection. The running stories will go into more details of the different ways to run stories. Or if you want to learn more about JBehave's step matching mechanism, you'll want to explore the concept of candidate steps in more detail.