In a nutshell, it is -“Linking User Stories and Domain-driven design to testing.” In simple words, it is a methodology for doing development on the basis of the system’s desired behavior. This a new thinking which brings all the stack holders (developer, business analyst, and Business users) together to work in a collaborative manner, which results in a better and robust application.
In BDD, Business Analyst breaks requirements into small User Stories, and each User Story into multiple scenarios.
Why BDD came into existence?
In earlier days, For development, Test Driven Development (TDD) methodology was used. Some improvements were needed in the TDD approach. Following are those improvements-
From Developer’s perspective – Developer always wanted to know –
- Where to start?
- What to test and what not to?
- How much to test in one go?
- What to call a test?
- How to understand why a test fails?
From the Business Analyst’s Perspective –
- The business analyst didn’t have a way to know how development is progressing.
What are the Benefits of BDD?
- It acts as a contract between the implementation team and the analysis team.
- A developer is highly focused because they know they need to start implementing the story. It results in rapid development.
- Scenarios can be grouped based on functionality rather than based on code components.
- Best fit for Agile Methodology.
BDD Story Template:
How to Implement BDD in Development?
In Java, JBehave is the API to implement BDD.
In .Net, SpecFlow is the tool to implement BDD.
In C, CBehave is the tool to implement BDD.
Introduction to JBehave:
Jbehave is a tool for implementing Behavior Driven Development (BDD) in Java.
- JBehave Provides API for following –
- Running the Stories written by BA.
- Read input parameters provided in the story file and passes it to test the code by using Parameter Injection Feature. Jbehave also provides some Built-In Parameter Converters.
- It provides annotations for fast test programming.
- Generates Report (Console, Text, HTML, XML) for the BA, which shows how many stories and scenarios are Passes/Failed/Pending. If any failure is there, it also prints the reason.
- JBehave provides a plug-in for Eclipse and easily configurable using Maven builds.
Advantages of JBehave:
- Developers are now more focused on development, which results in rapid implementation.
- It sets Responsibility on BA to put correct scenarios.
- Business Analyst has more visibility of how the development is progressing, and they can verify that their product is as per requirements or not by looking at the report.
- It reduces the chance of missed requirements and over code.
- Reduces communication efforts between BA and Dev team in clarifying the scenarios.
Running Story Files:
JBehave’s design is highly embeddable in its configuration and execution. At its heart, The Embedder separates the configuration of the story run from the matching of java methods with the textual story steps:
- Configuration instances are useful for the configuration of story execution.
- CandidateSteps instances help find the Java methods which match the textual steps in the story files.
The Embedder uses the list of CandidateSteps and the Configuration. It has sensible defaults for the configuration, for example, MostUsefulConfiguration.
Locating Stories: In JBehave, stories can be found locally, i.e., in the classpath, or externally, locatable via URLs. Depending on where the stories present, an appropriate StoryLoader needs to be defined:
- LoadFromClasspath for stories located in the classpath.
- LoadFromURL for stories found via URL.
JBehave allows multiple path configuration of the Embedder so that users can choose the one that best fits their needs.
It is the place we need to write the glue code which gets executed for each step in the story file. In simple terms, it decodes the text in story file and executes desired business logic.
Here @Given(“a trade of type $type”) is a candidate step which corresponds to “Given a trade of type New” in the story file.
JBehave uses method annotations (for example – @Given, @Then, @When) in Java step classes to link a Java method to a StepCandidate. Each step candidate corresponds to one Java method and one StepType. A step candidate holds the regex pattern contained in the annotation value, which is useful to match the textual steps in the scenarios.
A step candidate creation happens for each method annotated with one of the step annotations. Note that each method can support aliases and a different step candidate creation for each alias.
So, given one or more steps class instances, each containing one or more annotated methods, JBehave collects a list of step candidates, which is useful to match the textual steps found in the scenarios. For each given StepType, the regex pattern must be unique.
It is a strong recommendation to define your steps instances as POJOs and to not extend the Steps class directly. This allows you not to have any tie-ins with the Steps class implementation and also to use dependency injection to compose your steps instances using your preferred container.
The following screenshot shows the sample report generated by JBehave –
Business Analyst and Users can easily understand JBehave’s execution summary report. JBehave implements BDD in a simple way and gives us functional test coverage of the application.