Create Gherkin feature file & Generate Class

Gherkin is heart of Cucumber testing, here we are not going to describe Gherkin, if you like to know about it then please check Gherkin docs.

Gherkin feature file & Description

This tutorial will show you, how to 2 number addition by google calculator automatically.

Example Gherkin feature file for 2 number addition: This example will show you, open a browser & go to google.com, put 2 number with addition (+) sign, press enter or click to search then match the result, It will run 3 times with different values. Sounds like interesting right? stay and read or see the video until finish the tutorial.

Feature: Test addition using Google Calculator


  Scenario Outline: Test 2 number addition
    Given Open google.com
    When Entering number <firstNumber> and <secondNumber>
    And Press enter
    Then Result should be <result>

    Examples:
      | firstNumber | secondNumber | result |
      | 6           | 6            | 12     |
      | 6           | 2            | 9      |
      | 8           | 5            | 13     |

Let’s add the Gherkin feature file into project

  1. Click Projects from your IntelliJ IDEA Community Edition left side.

  2. Find the location src/test/resources.

  3. Right click on the location and New >> Directory.

  4. Add the name com.hmtmcse.tutorial or your preferred one, but please remember this one.

  5. Again right click on com.hmtmcse.tutorial, New >> File

  6. Enter the name google.feature and copy paste the above feature content.

Generate Class from feature file

After create Gherkin feature file we need to generate Java Definition file for write test functionality. It’s very much easy. Now giving you step by step process list.

  • Go to the feature file called google.feature.

  • Right click on the feature file and click on Run or ctrl + shift + F10.

  • If everything is okay then see the console log, like below:

@Given("Open google.com")
public void open_google_com() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@When("Entering number {int} and {int}")
public void entering_number_and(Integer int1, Integer int2) {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@When("Press enter")
public void press_enter() {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}

@Then("Result should be {int}")
public void result_should_be(Integer int1) {
    // Write code here that turns the phrase above into concrete actions
    throw new io.cucumber.java.PendingException();
}
  • Copy the above line, then go to src/test/java.

  • Create package called com.hmtmcse.tutorial or your preferred one, but please remember this one. (Right click top of java then New >> Package)

  • Now create class called StepDefinitions. (Right click top of com.hmtmcse.tutorial then New >> Java Class)

  • Paste the copied codes on to StepDefinitions.

  • I am rewriting the full class and now it look like below.

import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;

public class StepDefinitions {

    @Before
    public void setup() {
        System.out.println("Setup Somethings");
    }

    @Given("Open google.com")
    public void open_google_com() {
        System.out.println("------------------------------------------------------------");
        System.out.println("Open Google");
    }

    @When("Entering number {int} and {int}")
    public void entering_number_and(Integer first, Integer second) {
        System.out.println("First " + first + " Second " + second);
    }

    @When("Press enter")
    public void press_enter() {
        System.out.println("Press enter");
    }

    @Then("Result should be {int}")
    public void result_should_be(Integer result) {
        System.out.println("Result " + result);
        System.out.println("============================================================");
    }

    @After
    public void end() {
        System.out.println("Process End");
    }

}

Here added 2 new method with annotation: This 2 methods has some special work will see later.

  • @Before: Run the method before the test started.

  • @After: Execute this when the task completed.

Run & Test the Class

  • From Projecs find the file called build.gradle.

  • There will be a task called cucumber, run this task clicking on left side run icon.

YouTube Video Tutorial of Gherkin Feature and Class