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
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 the InjectableStepsFactory
Configuration configuration = ... // optional configuration InjectableStepsFactory factory = new InstanceStepsFactory(configuration, new TraderSteps(), new BeforeAndAfterSteps()); factor.createCandidateSteps();
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.
In JBehave, textual stories can be run in an automated way via a mapping to Java runnable classes.
JBehave allows many different ways to provide the mapping of textual stories to runnable Java 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 provides two main Embeddable implementations:
JUnit is supported out-of-the-box via several Embeddables implementations:
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:
JBehave also provides an implementation of JUnit's Runner, AnnotatedEmbedderRunner, which is runnable via JUnit's @RunWith annotation:
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: