Behavior Driven Development with JUnit 5. Part 3

Behavior Driven Development with JUnit 5. Part 3

The third part of our article on Behavior Driven Development with JUnit 5. Happy reading.

4. Working BDD style with Cucumber and JUnit 5


In our previous article, we worked TDD style to develop the flight-management application to a stage where it can work with three types of flights: economy, business, and premium. In addition, we implemented the requirement that a passenger can be added only once to a flight.

We have already introduced, in a discrete way, a first taste of the BDD way of working. We can easily read how the application works by following the tests using the Given, When, and Then keywords. We’ll move the application to BDD with Cucumber and also introduce more new features.

Cucumber is a BDD testing tool framework. It describes application scenarios in plain English using a language called Gherkin. Cucumber is easy for stakeholders to read and understand and allows automation.

The main capabilities of Cucumber are as follows:

  • Scenarios or examples describe the requirements
  • A scenario is defined through a list of steps to be executed by Cucumber
  • Cucumber executes the code corresponding to the scenarios, checks that the software follows these requirements, and generates a report describing the success or failure of each scenario

The main capabilities of Gherkin are as follows:

  • Gherkin defines simple grammar rules that allow Cucumber to understand plain English text
  • Gherkin documents the behavior of the system. The requirements are always up to date, as they are provided through scenarios that represent living specifications

Cucumber ensures that technical and non-technical people can easily read, write, and understand the acceptance tests. The acceptance tests became an instrument of communication between the stakeholders of the project.

A Cucumber acceptance test looks like this:

Given there is an economy flight
When we have a regular passenger
Then you can add and remove him from an economy flight

Again, notice the Given/When/Then keywords for describing a scenario, which we introduced in our previous work with JUnit 5. We’ll no longer use them just for labeling: Cucumber interprets sentences starting with these keywords and generates methods that it annotates using the annotations @Given, @When, and @Then.

The acceptance tests are written in Cucumber feature files. A feature file is an entry point to the Cucumber tests; in the file, we describe our tests in Gherkin. A feature file can contain one or many scenarios.

We can make plans for starting to work with Cucumber in the project. We’ll first introduce the Cucumber dependencies into the existing Maven configuration. We’ll create a Cucumber feature and generate the skeleton of the Cucumber tests. Then, we’ll move the existing JUnit 5 tests to fill in this Cucumber-generated test skeleton.

<dependency>

     <groupId>info.cukes</groupId>

     <artifactId>cucumber-java</artifactId>

     <version>1.2.5</version>

     <scope>test</scope>

</dependency>

<dependency>

     <groupId>info.cukes</groupId>

     <artifactId>cucumber-junit</artifactId>

     <version>1.2.5</version>

     <scope>test</scope>

</dependency>


In this listing, we introduce the two needed Maven dependencies: cucumber-java and cucumber-junit.

We’’ill begin creating Cucumber features. We follow the Maven standard folder structure and introduce the features into the test/resources folder. We create the test/resources/features folder and, in it, we create the passengers_policy.feature file (figure 1).

passengers_policy_feature.png

Figure 1 The new Cucumber passenger_policy.feature file is created in the test/resources/features folder, following Maven rules.


We follow the Gherkin syntax and introduce a feature named Passengers Policy, together with a short description of what it should do. Then we follow the Gherkin syntax to write scenarios.

Feature: Passengers Policy
The company follows a policy of adding and removing passengers, depending on the passenger type and on the flight type

Scenario: Economy flight, regular passenger
Given there is an economy flight
When we have a regular passenger
Then you can add and remove him from an economy flight
And you cannot add a regular passenger to an economy flight more than once

Scenario: Economy flight, VIP passenger
Given there is an economy flight
When we have a VIP passenger
Then you can add him but cannot remove him from an economy flight
And you cannot add a VIP passenger to an economy flight more than once

Scenario: Business flight, regular passenger
Given there is a business flight
When we have a regular passenger
Then you cannot add or remove him from a business flight

Scenario: Business flight, VIP passenger
Given there is a business flight
When we have a VIP passenger
Then you can add him but cannot remove him from a business flight
And you cannot add a VIP passenger to a business flight more than once

Scenario: Premium flight, regular passenger
Given there is a premium flight
When we have a regular passenger
Then you cannot add or remove him from a premium flight

Scenario: Premium flight, VIP passenger
Given there is a premium flight
When we have a VIP passenger
Then you can add and remove him from a premium flight
And you cannot add a VIP passenger to a premium flight more than once

Interested in JUnit? Check out our trainings.

Catalin Tudose
Java and Web Technologies Expert

Mai ai întrebări?
Conectați-văcu noi