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
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:
- Must extend an instance of a RunnableScenario, such as JUnitScenario
- 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()); } }
- 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:
- MostUsefulConfiguration : provides default configuration that most user will find appropriate
- PropertyBasedConfiguration : overrides default configuration via system properties
Configuring Steps
Steps can also be configured to a high degree via the StepsConfiguration .
Among the elements that can be configured are:
- StepPatternBuilder defaults to PrefixCapturingPatternBuilder
- StepMonitor : defaults to SilentStepMonitor , useful to either debug the step matching or to describe the steps being performed to some output
- ParameterConverters : facade for collecting user-defined ParameterConverter .
