There are few alternatives to choosing a BDD framework. and we’ll also take a look at the very popular one, JBahave. As implementing the idea of behaviour-driven development, JBehave allows us to write stories in plain text to be understood by all persons involved in the project. Through the stories, we’ll define scenarios that express the desired behaviour. Then, steps need to be mapped to a Java POJO. JBehave maps textual steps to Java methods via candidate steps. The scenario writer needs to provide annotated methods that match by regex patterns the textural steps.
Configure stories. JBehave runs the stories with an embedder, an entry point to all JBehave functionalities that are embeddable into launchers such as IDEs or CLIs. JBehave complements the embedder with an embeddable, which represents a runnable facade to the embedder. Finally, the JBehave core module contains support for running stories as JUnit tests. As with other BDD frameworks, JBehave provides its terminology. We have to mention here, a story. This represents an automatically executable increment of business functionality, a slice through a feature that is functional, and on which we can get feedback. It covers one or more scenarios.
A scenario - this represents a real-life situation of the behaviour of the application. A step is a define using the classic BDD keywords: Given, When and Than.
Working with code
We use some application that simulates some BDD approach bu using JUnit 5 facilities and annotations, and the tests have been rewritten with this purpose in mind. Below is the code:
JBehave Installation and configuration
First, in order to work with JBehave, we’ll install the plugin for IntelliJ. We’ll choose JBehave Support and JBehave Step Generator.
Plugins: JBehave Support & JBehave Generator
First story file
To add story, we should add story file into test resources folder and called it: <name_of_file>.story
Base on this file we can generate a java file that contains all the steps. We write the scenario and translate it into code. Given there is an economy flight, when we have a usual passenger, then you can add or remove him from an economy flight.
In order to be able to run these tests, we need a new special class that will represent the test configuration. This class will extend JUnitStory and for the moment it will tell us the following, that is configuration is the most useful configuration, and to display the report on the console. The steps definition is to be found in the PassengerPlicy class.