Writing Textual Scenarios

Behaviour-Driven Development encourages you to start defining the 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
    When the stock is traded at 11.0
    Then the alert status should be ON

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.scenario.

The scenario should use a syntax compatible with the Grammar.

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. One way this can be done is by extending the default implementation Steps:

public class TraderSteps extends Steps {

    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));
    }

}

Equivalently, as composition is often preferrable to inheritance, the scenario writer can use a POJO (i.e. without extending 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));
    }

}

If POJOs are used we need to create CandidateSteps via the StepsFactory

    StepsConfiguration configuration = ... // optional configuration
    new StepsFactory(configuration).createCandidateSteps(new TraderSteps()));    

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 class 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.

Mapping Scenario Files to Java Classes

In JBehave Core scenarios can be run in an automated way via a one-to-one mapping to Java classes. The mechanism via which the textual scenarios are resolved from a Java class is determined by the implementation of the ScenarioNameResolver. In our example, we need to create a file TraderIsAletedOfStatus.java, which maps to out textual scenario in same package.

The contract for a user-defined Java scenario is the following:

Must extend an instance of a RunnableScenario, such as JUnitScenario

Must provide a default constructor.

(Only when running with external resources) Must provide an additional constructor with a ClassLoader parameter.

Thus in our case the example Scenario would look like:

public class TraderScenario extends JUnitScenario {

    public TraderScenario() {
        super(new MostUsefulConfiguration() {
            public ScenarioDefiner forDefiningScenarios() {
                return new ClasspathScenarioDefiner(new UnderscoredCamelCaseResolver(".scenario"), 
                             new PatternScenarioParser(keywords()));
            }
        });
        addSteps(new TraderSteps()); // if TraderSteps extends Steps
        addSteps(new StepsFactory().createCandidateSteps(new TraderSteps())); // if TraderSteps is a POJO
    }
}

Here we are configuring our textual scenario files to end with extension .scenario, by overriding the default behaviour of UnderscoredCamelCaseResolver which has no extension.

What Next?

The Configuring Scenarios page will go into more configuration details and Running Scenarios into the different ways to run scenarios. Or if you want to learn more about JBehave's step matching mechanism, you'll want to explored the concept of candidate steps in more detail.