Test Scenario using Cucumber-JUnit

In this tutorial, we will write our first test scenario using cucumber tool. The testing framework used with cucumber in this tutorial is Junit.

After creating a project in eclipse add below jar files into the project:

  • cucumber-junit
  • cucumber-reporting
  • junit

You can download these jar files from Maven website. (https://mvnrepository.com/)

If you have created a Maven project add below dependencies:

<!-- https://mvnrepository.com/artifact/info.cukes/cucumber-junit -->

<!-- https://mvnrepository.com/artifact/junit/junit -->

<!-- https://mvnrepository.com/artifact/org.uniknow.agiledev/cucumber-reporting -->

Create a feature file:

Create a Java project or Maven java project in eclipse.

Create a folder with the name “feature”.

Create a feature file say with name Login.feature. Right-click project name >> new folder with name feature >> new feature file with name Login.feature.

What is a feature file?

A “feature” file is a plain text file with the .feature extension. We write test cases in the feature file using Gherkin language.

Feature file consists of the following components –

Feature: Feature keyword describes what we are going to test in the test scripts.

Scenario: Scenario keyword describes a particular test scenario to test in a feature.

Scenario Outline: A scenario can be executed for multiple sets of data using the scenario outline. The data is provided by a tabular structure separated by (||).

Given: It specifies the context of the text to be executed.

When: “When” specifies the test action that has to be performed.

Then: The expected outcome of the test can be represented by “Then”.

Let us see an example of a feature file and then we will understand each statement:

Feature: Login to facebook
  In this feature we will login to facebook website.

  Scenario: Login with correct credentials.
    Given I navigate to login page of facebook website
    When I entered email Id as saurabh.d2106@gmail.com
    And I entered password as QA@1234
    Then I navigated to homepage of facebook website

In the above code, the test case is written in plain English language. The code starts with Feature keyword which tells about what feature this file consists of and the next statement has a description of the feature.

Next, is the Scenario keyword which is the first test scenario. A feature file has multiple scenarios.

After that, we write the steps to execute in the scenario using tags like Given, When, And, Then, But, etc.

When you will create the feature files it will show you a warning with each step stating – Step <step> does not have a matching glue code. See the below screenshot.

To remove these error let us first understand –

How does Cucumber tool work?

– Cucumber first reads the feature files and identify all the steps in that feature file.
– Then Cucumber maps these steps with the step-definition (using TestRunner class).
– Then Cucumber executes these steps of step definition on the Application.

Create a Test Runner:

Next, we will write a code which will execute this code written above.

  • Create a package with name testRunner.
  • Create a class with the name TestRunner.java.
    • In TestRunner.java class add an annotation @RunWith(Cucumber.class) where @RunWith is from org.junit.runner.RunWith class and Cucumber.class is from cucumber.api.junit.Cucumber class.
    • Add @CucumberOptions annotation – this informs test engine with some properties
      • informs the test engine where are the feature files.
      • glue – It is to bind the feature files with the step definition

See the below code:

package testRunner;

import org.junit.runner.RunWith;

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@CucumberOptions(features = "features", glue = { "steps" })
public class TestRunner {


Create a Step Definition class:

Next step is to create a Step definition class in steps package. Right-click steps package >> new >> class >> LoginSteps.java.

Right click on TestRunner.java class >> Run As >> JUnit Test cases. It will execute the code and will show you some message in the console. like below:

1 Scenarios (1 undefined)
4 Steps (3 undefined, 1 passed)

You can implement missing steps with the snippets below:

@Given("^I entered email Id as saurabh\\.d(\\d+)@gmail\\.com$")
public void i_entered_email_Id_as_saurabh_d_gmail_com(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();

@Given("^I entered password as QA@(\\d+)$")
public void i_entered_password_as_QA(int arg1) throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();

@Then("^I navigated to homepage of facebook website$")
public void i_navigated_to_homepage_of_facebook_website() throws Throwable {
    // Write code here that turns the phrase above into concrete actions
    throw new PendingException();

Copy all the steps from above and paste them into LoginSteps.java class and edit each step. You can also change the name of the methods. See the below code:

package steps;

import cucumber.api.java.en.And;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import gherkin.lexer.Pa;

public class LoginSteps {
	@Given("^I navigate to login page of facebook website$")
	public void invokeBrowser() throws Throwable {
		System.out.println("I navigate to Facebook website");
	@And("^I entered email Id as (.*)$")
	public void enterEmailId(String emailId) throws Throwable {
	  System.out.println("I enter email Id as "+emailId);

	@And("^I entered password as (.*)$")
	public void enterPassword(String password) throws Throwable {
		System.out.println("I enter password as "+ password);

	@Then("^I navigated to homepage of facebook website$")
	public void verifyHomePage() throws Throwable {
	    System.out.println("I verified the home page");


Wherever there is (.*) symbol it is a special character and is used to pass a parameter in a test step. Like in above code email Id and password are passed as an argument in the test step.

We will learn about these special characters in detail in upcoming tutorials.

Run the code again and you will get below output:

I navigate to Facebook website
I enter email Id as saurabh.d2106@gmail.com
I enter password as password
I verified the home page

PS: For any questions, queries or feedback. Feel free to write us at saurabh@qatechhub.com or support@qatechhub.com

Saurabh Dhingra

About the Author

Saurabh Dhingra

Follow Saurabh Dhingra: