Writing the scenario

Cucumber is a popular BDD test automation tool. Cucumber-JVM is the Java implementation of Cucumber. In Cucumber, you express acceptance criteria in a natural, human-readable form. In Cucumber, scenarios are stored in Feature Files, which contains an overall description of a feature as well as a number of scenarios. For example there is feature file called search_by_keyword.feature, and looks something like this:

Feature: Searching by keyword
  In order to find items that I would like to purchase
  As a potential buyer
  I want to be able to search for items containing certain words
  Scenario: Should list items related to a specified keyword
    Given I want to buy a wool scarf
    When I search for items containing 'wool'
    Then I should only see items related to ‘wool’

Step definitions

In Cucumber, each line of the Gherkin scenario maps to a method in a Java class, known as a Step Definition. These use annotations like @Given, @Whenand @Then, that match lines in the scenario to Java methods. You define simple expressions to indicate parameters that will be passed into the methods:

public class SearchByKeywordStepDefinitions {
    BuyerSteps buyer;
    @Given("I want to buy (.*)")
    public void buyerWantsToBuy(String article) {
    @When("I search for items containing '(.*)'")
    public void searchByKeyword(String keyword) {
    @Then("I should only see items related to '(.*)'")
    public void resultsForACategoryAndKeywordInARegion(String keyword) {

“BDD Test Automation with Cucumber-JVM” Tech Bite was brought to you by Medžida Mustafić, Junior Test Engineer at Atlantbh. 

Tech Bites are tips, tricks, snippets or explanations about various programming technologies and paradigms, which can help engineers with their everyday job.

Leave a Reply