Developing Scenarios

Writing Text Scenarios

We encourage users to start from writing your scenarios in a
non-Java format, e.g. in simple text:

    Given a stock of price 1.0 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
and to give a name that is expressive, i.e. status_alert_can_be_activated.scenario. This scenario will map to a Java file StatusAlertCanBeActivated.java in same package.

Writing Java Scenarios

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

  1. Must extend an instance of a RunnableScenario, such as JUnitScenario
  2. Must provide a default constructor and a constructor with a ClassLoader parameter, e.g.:

       public class StatusAlertCanBeActivated extends JUnitScenario {
     
        public StatusAlertCanBeActivated() {
            this(Thread.currentThread().getContextClassLoader());
        }
     
        public StatusAlertCanBeActivated(final ClassLoader classLoader) {
            super(new MostUsefulConfiguration() {
                public ScenarioDefiner forDefiningScenarios() {
                    return new ClasspathScenarioDefiner(
                        new UnderscoredCamelCaseResolver(".scenario"), 
                        new PatternScenarioParser(
                           new PropertyBasedConfiguration()), classLoader);
                }
            }, new TraderSteps());
        }
     
    }

  3. Must inject an instance of CandidateSteps e.g.:

       public class TraderSteps extends Steps {
     
        private Stock stock;
     
        @Given("a stock of price $price and a threshold of $threshold")
        public void aStockOfPrice(double price, double threshold) {
            stock = new Stock(price, 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));
        }
     
    }



Configuring Scenarios

JBehave was designed to be highly configurable but to provide
default behaviour for the most useful configuration. Configuration
is the main interface for configuring all the components of a scenario.
JBehave provides two implementations:


Configuring Steps

Steps can also be configured to a high degree via the StepsConfiguration .
Among the elements that can be configured are: