BDD is a set of practices that helps to reduce the rework caused by misunderstanding or vague requirements, narrow the communication gaps between the development team, testing team, and customers, and promote continuous communication among them. Cucumber is one such open-source tool, which supports Behaviour Driven Development(BDD). In simple words, Cucumber can be defined as a testing framework, driven by plain English. It serves as documentation, automated tests, and a development aid – all in one.
Cucumber Introduction, Installation, and Configuration
A Step Definition is a Java method with an expression that links it to one or more Gherkin steps. When Cucumber executes a Gherkin step in a scenario, it will look for a matching step definition to execute.
Cucumber finds the Step Definition file with the help of the Glue code in Cucumber Options.
By storing state in instance variables, a step definition can transfer state to a subsequent step definition.
Step definitions are not associated with a specific feature file or scenario. The name of a step definition’s file, class, or package has no bearing on which Gherkin steps it will match. The formulation of the step definition is the only thing that matters, which means the step definition should only match Gherkin’s steps.
Imagine, we want to test a web application. One of the first steps is Login to the website and then check the various functionalities on the website. We can create a Gherkin step like “I login to the website” and the corresponding step definition of this Gherkin Step. This Gherkin step can be used in multiple feature files, and we don’t need to create the step definition of this Gherkin step for each feature file.
In the previous tutorial, we have seen that when the Feature file is executed without the Step Definition file, the runner shows the missing steps with the snippet in the console.
When a Cucumber encounters a Gherkinstep without a matching step definition, it will print a step definition snippet with a matching Cucumber Expression. You can use this as a starting point for new step definitions.
It is very easy to implement all the steps, all you need to do is copy the complete text marked in the above box and paste it into the MyHolidayDefinitions class.
@Given, @When, and @Then are imported from packages:-
Feature: Book flight ticket
@BookOneWayFlight
Scenario: Book Flight for one way trip
Given I live in Dublin with 2 adults and 2 kids
And I want to book one way flight ticket from Dublin to London on 22 Jan 2020
When I search online
Then TripAdvisor should provide me options of flights on 22 Jan 2020
And Cost of my flight should not be more than 50 Euro per person
And Tickets should be refundable
Let me create the step definition for the above Feature file
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
public class MyHolidayDefinitions {
@Given("I live in Dublin with {int} adults and {int} kids")
public void liveInDublin(Integer int1, Integer int2) {
System.out.println("I live in Dublin with 2 adults and 2 kids");
}
@Given("I want to book one way flight ticket from Dublin to London on {int} Jan {int}")
public void bookFlightTicket(Integer int1, Integer int2) {
System.out.println("I want to book one way flight ticket from Dublin to London on 22 Jan 2020");
}
@When("I search online")
public void searchOnline() {
System.out.println("I search online");
}
@Then("TripAdvisor should provide me options of flights on {int} Jan {int}")
public void tripAdvisor(Integer int1, Integer int2) {
System.out.println("TripAdvisor should provide me options of flights on 22 Jan 2020");
}
@Then("Cost of my flight should not be more than {int} Euro per person")
public void costOfFlightLimit(Integer int1) {
System.out.println("Cost of my flight should not be more than 50 Euro per person");
}
@Then("Tickets should be refundable")
public void refundableTickets() {
System.out.println("Tickets should be refundable");
}
}
To run the scenarios present in the Feature File, we need TestRunner class. To learn more about the TestRunner class, please refer to this tutorial – Cucumber Tutorial – JUnit Test Runner Class